summaryrefslogtreecommitdiff
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.el88
1 files changed, 79 insertions, 9 deletions
diff --git a/lisp/org/org-src.el b/lisp/org/org-src.el
index 7876deaba19..28733d0115b 100644
--- a/lisp/org/org-src.el
+++ b/lisp/org/org-src.el
@@ -148,6 +148,9 @@ the existing edit buffer."
"How the source code edit buffer should be displayed.
Possible values for this option are:
+plain Show edit buffer using `display-buffer'. Users can
+ further control the display behavior by modifying
+ `display-buffer-alist' and its relatives.
current-window Show edit buffer in the current window, keeping all other
windows.
split-window-below Show edit buffer below the current window, keeping all
@@ -156,10 +159,12 @@ split-window-right Show edit buffer to the right of the current window,
keeping all other windows.
other-window Use `switch-to-buffer-other-window' to display edit buffer.
reorganize-frame Show only two windows on the current frame, the current
- window and the edit buffer. When exiting the edit buffer,
- return to one window.
+ window and the edit buffer.
other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
- Also, when exiting the edit buffer, kill that frame."
+ Also, when exiting the edit buffer, kill that frame.
+
+Values that modify the window layout (reorganize-frame, split-window-below,
+split-window-right) will restore the layout after exiting the edit buffer."
:group 'org-edit-structure
:type '(choice
(const current-window)
@@ -232,11 +237,11 @@ green, respectability.
:version "26.1"
:package-version '(Org . "9.0"))
-(defcustom org-src-tab-acts-natively nil
+(defcustom org-src-tab-acts-natively t
"If non-nil, the effect of TAB in a code block is as if it were
issued in the language major mode buffer."
:type 'boolean
- :version "24.1"
+ :package-version '(Org . "9.4")
:group 'org-babel)
@@ -276,6 +281,9 @@ issued in the language major mode buffer."
(defvar-local org-src--remote nil)
(put 'org-src--remote 'permanent-local t)
+(defvar-local org-src--saved-temp-window-config nil)
+(put 'org-src--saved-temp-window-config 'permanent-local t)
+
(defvar-local org-src--source-type nil
"Type of element being edited, as a symbol.")
(put 'org-src--source-type 'permanent-local t)
@@ -355,6 +363,12 @@ where BEG and END are buffer positions and CONTENTS is a string."
(end (progn (goto-char (org-element-property :end datum))
(search-backward "}" (line-beginning-position) t))))
(list beg end (buffer-substring-no-properties beg end))))
+ ((eq type 'latex-fragment)
+ (let ((beg (org-element-property :begin datum))
+ (end (org-with-point-at (org-element-property :end datum)
+ (skip-chars-backward " \t")
+ (point))))
+ (list beg end (buffer-substring-no-properties beg end))))
((org-element-property :contents-begin datum)
(let ((beg (org-element-property :contents-begin datum))
(end (org-element-property :contents-end datum)))
@@ -469,6 +483,10 @@ When REMOTE is non-nil, do not try to preserve point or mark when
moving from the edit area to the source.
Leave point in edit buffer."
+ (when (memq org-src-window-setup '(reorganize-frame
+ split-window-below
+ split-window-right))
+ (setq org-src--saved-temp-window-config (current-window-configuration)))
(let* ((area (org-src--contents-area datum))
(beg (copy-marker (nth 0 area)))
(end (copy-marker (nth 1 area) t))
@@ -540,6 +558,10 @@ Leave point in edit buffer."
(setq org-src-source-file-name source-file-name)
;; Start minor mode.
(org-src-mode)
+ ;; Clear undo information so we cannot undo back to the
+ ;; initial empty buffer.
+ (buffer-disable-undo (current-buffer))
+ (buffer-enable-undo)
;; Move mark and point in edit buffer to the corresponding
;; location.
(if remote
@@ -792,9 +814,14 @@ Raise an error when current buffer is not a source editing buffer."
(defun org-src-switch-to-buffer (buffer context)
(pcase org-src-window-setup
+ (`plain
+ (when (eq context 'exit) (quit-restore-window))
+ (pop-to-buffer buffer))
(`current-window (pop-to-buffer-same-window buffer))
(`other-window
- (switch-to-buffer-other-window buffer))
+ (let ((cur-win (selected-window)))
+ (org-switch-to-buffer-other-window buffer)
+ (when (eq context 'exit) (quit-restore-window cur-win))))
(`split-window-below
(if (eq context 'exit)
(delete-window)
@@ -912,7 +939,7 @@ A coderef format regexp can only match at the end of a line."
;; remove any newline characters in order to preserve
;; table's structure.
(when (org-element-lineage definition '(table-cell))
- (while (search-forward "\n" nil t) (replace-match "")))))
+ (while (search-forward "\n" nil t) (replace-match " ")))))
contents
'remote))
;; Report success.
@@ -942,6 +969,46 @@ Throw an error when not at such a table."
(table-recognize)
t))
+(defun org-edit-latex-fragment ()
+ "Edit LaTeX fragment at point."
+ (interactive)
+ (let ((context (org-element-context)))
+ (unless (and (eq 'latex-fragment (org-element-type context))
+ (org-src--on-datum-p context))
+ (user-error "Not on a LaTeX fragment"))
+ (let* ((contents
+ (buffer-substring-no-properties
+ (org-element-property :begin context)
+ (- (org-element-property :end context)
+ (org-element-property :post-blank context))))
+ (delim-length (if (string-match "\\`\\$[^$]" contents) 1 2)))
+ ;; Make the LaTeX deliminators read-only.
+ (add-text-properties 0 delim-length
+ (list 'read-only "Cannot edit LaTeX deliminator"
+ 'front-sticky t
+ 'rear-nonsticky t)
+ contents)
+ (let ((l (length contents)))
+ (add-text-properties (- l delim-length) l
+ (list 'read-only "Cannot edit LaTeX deliminator"
+ 'front-sticky nil
+ 'rear-nonsticky nil)
+ contents))
+ (org-src--edit-element
+ context
+ (org-src--construct-edit-buffer-name (buffer-name) "LaTeX fragment")
+ (org-src-get-lang-mode "latex")
+ (lambda ()
+ ;; Blank lines break things, replace with a single newline.
+ (while (re-search-forward "\n[ \t]*\n" nil t) (replace-match "\n"))
+ ;; If within a table a newline would disrupt the structure,
+ ;; so remove newlines.
+ (goto-char (point-min))
+ (when (org-element-lineage context '(table-cell))
+ (while (search-forward "\n" nil t) (replace-match " "))))
+ contents))
+ t))
+
(defun org-edit-latex-environment ()
"Edit LaTeX environment at point.
\\<org-src-mode-map>
@@ -1182,8 +1249,11 @@ Throw an error if there is no such buffer."
(write-back (org-src--goto-coordinates coordinates beg end))))
;; Clean up left-over markers and restore window configuration.
(set-marker beg nil)
- (set-marker end nil)))
-
+ (set-marker end nil)
+ (when org-src--saved-temp-window-config
+ (unwind-protect
+ (set-window-configuration org-src--saved-temp-window-config)
+ (setq org-src--saved-temp-window-config nil)))))
(provide 'org-src)