summaryrefslogtreecommitdiff
path: root/lisp/org/org-src.el
diff options
context:
space:
mode:
authorBastien <bzg@gnu.org>2019-12-03 23:27:04 +0100
committerBastien <bzg@gnu.org>2019-12-03 23:27:04 +0100
commit165f7383822086d465519ebe6e4283723923f097 (patch)
tree820be9480e3d571d766483f564c963037192f6ec /lisp/org/org-src.el
parent821de968434d2096bdea67dd24301bf6b517aef1 (diff)
downloademacs-165f7383822086d465519ebe6e4283723923f097.tar.gz
emacs-165f7383822086d465519ebe6e4283723923f097.tar.bz2
emacs-165f7383822086d465519ebe6e4283723923f097.zip
Update Org to 9.3
Diffstat (limited to 'lisp/org/org-src.el')
-rw-r--r--lisp/org/org-src.el249
1 files changed, 153 insertions, 96 deletions
diff --git a/lisp/org/org-src.el b/lisp/org/org-src.el
index d8f2cefe181..5e50a1b47cb 100644
--- a/lisp/org/org-src.el
+++ b/lisp/org/org-src.el
@@ -32,13 +32,11 @@
;;; Code:
(require 'cl-lib)
+(require 'ob-comint)
(require 'org-macs)
(require 'org-compat)
-(require 'ob-keys)
-(require 'ob-comint)
+(require 'org-keys)
-(declare-function org-base-buffer "org" (buffer))
-(declare-function org-do-remove-indentation "org" (&optional n))
(declare-function org-element-at-point "org-element" ())
(declare-function org-element-class "org-element" (datum &optional parent))
(declare-function org-element-context "org-element" (&optional element))
@@ -48,9 +46,6 @@
(declare-function org-element-type "org-element" (element))
(declare-function org-footnote-goto-definition "org-footnote"
(label &optional location))
-(declare-function org-get-indentation "org" (&optional line))
-(declare-function org-switch-to-buffer-other-window "org" (&rest args))
-(declare-function org-trim "org" (s &optional keep-lead))
(defvar org-inhibit-startup)
@@ -128,7 +123,8 @@ editing it with `\\[org-edit-src-code]'.
It has no effect if `org-src-preserve-indentation' is non-nil."
:group 'org-edit-structure
- :type 'integer)
+ :type 'integer
+ :safe #'wholenump)
(defcustom org-edit-src-persistent-message t
"Non-nil means show persistent exit help message while editing src examples.
@@ -152,17 +148,23 @@ the existing edit buffer."
"How the source code edit buffer should be displayed.
Possible values for this option are:
-current-window Show edit buffer in 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.
-other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
- Also, when exiting the edit buffer, kill that frame."
+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
+ other windows.
+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.
+other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
+ Also, when exiting the edit buffer, kill that frame."
:group 'org-edit-structure
:type '(choice
(const current-window)
+ (const split-window-below)
+ (const split-window-right)
(const other-frame)
(const other-window)
(const reorganize-frame)))
@@ -179,17 +181,29 @@ or similar things which you want to have when editing a source code file,
but which mess up the display of a snippet in Org exported files.")
(defcustom org-src-lang-modes
- '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
- ("asymptote" . asy) ("dot" . fundamental) ("sqlite" . sql)
- ("calc" . fundamental) ("C" . c) ("cpp" . c++) ("C++" . c++)
- ("screen" . shell-script) ("shell" . sh) ("bash" . sh))
+ '(("C" . c)
+ ("C++" . c++)
+ ("asymptote" . asy)
+ ("bash" . sh)
+ ("beamer" . latex)
+ ("calc" . fundamental)
+ ("cpp" . c++)
+ ("ditaa" . artist)
+ ("dot" . fundamental)
+ ("elisp" . emacs-lisp)
+ ("ocaml" . tuareg)
+ ("screen" . shell-script)
+ ("shell" . sh)
+ ("sqlite" . sql))
"Alist mapping languages to their major mode.
-The key is the language name, the value is the string that should
-be inserted as the name of the major mode. For many languages this is
-simple, but for language where this is not the case, this variable
-provides a way to simplify things on the user side.
-For example, there is no ocaml-mode in Emacs, but the mode to use is
-`tuareg-mode'."
+
+The key is the language name. The value is the mode name, as
+a string or a symbol, without the \"-mode\" suffix.
+
+For many languages this is simple, but for language where this is
+not the case, this variable provides a way to simplify things on
+the user side. For example, there is no `ocaml-mode' in Emacs,
+but the mode to use is `tuareg-mode'."
:group 'org-edit-structure
:type '(repeat
(cons
@@ -229,23 +243,52 @@ issued in the language major mode buffer."
;;; Internal functions and variables
-(defvar org-src--allow-write-back t)
-(defvar org-src--auto-save-timer nil)
-(defvar org-src--babel-info nil)
-(defvar org-src--beg-marker nil)
-(defvar org-src--block-indentation nil)
-(defvar org-src--end-marker nil)
-(defvar org-src--from-org-mode nil)
-(defvar org-src--overlay nil)
-(defvar org-src--preserve-indentation nil)
-(defvar org-src--remote nil)
-(defvar org-src--saved-temp-window-config nil)
-(defvar org-src--source-type nil
+(defvar org-src--auto-save-timer nil
+ "Idle Timer auto-saving remote editing buffers.")
+
+(defvar-local org-src--allow-write-back t)
+(put 'org-src--allow-write-back 'permanent-local t)
+
+(defvar-local org-src--babel-info nil)
+(put 'org-src--babel-info 'permanent-local t)
+
+(defvar-local org-src--beg-marker nil)
+(put 'org-src--beg-marker 'permanent-local t)
+
+(defvar-local org-src--block-indentation nil)
+(put 'org-src--block-indentation 'permanent-local t)
+
+(defvar-local org-src--content-indentation nil)
+(put 'org-src--content-indentation 'permanent-local t)
+
+(defvar-local org-src--end-marker nil)
+(put 'org-src--end-marker 'permanent-local t)
+
+(defvar-local org-src--from-org-mode nil)
+(put 'org-src--from-org-mode 'permanent-local t)
+
+(defvar-local org-src--overlay nil)
+(put 'org-src--overlay 'permanent-local t)
+
+(defvar-local org-src--preserve-indentation nil)
+(put 'org-src--preserve-indentation 'permanent-local t)
+
+(defvar-local org-src--remote nil)
+(put 'org-src--remote 'permanent-local t)
+
+(defvar-local org-src--source-type nil
"Type of element being edited, as a symbol.")
-(defvar org-src--tab-width nil
+(put 'org-src--source-type 'permanent-local t)
+
+(defvar-local org-src--tab-width nil
"Contains `tab-width' value from Org source buffer.
However, if `indent-tabs-mode' is nil in that buffer, its value
is 0.")
+(put 'org-src--tab-width 'permanent-local t)
+
+(defvar-local org-src-source-file-name nil
+ "File name associated to Org source buffer, or nil.")
+(put 'org-src-source-file-name 'permanent-local t)
(defun org-src--construct-edit-buffer-name (org-buffer-name lang)
"Construct the buffer name for a source editing buffer."
@@ -264,21 +307,6 @@ Return nil if there is no such buffer."
(eq (marker-buffer end) (marker-buffer org-src--end-marker))
(throw 'exit b))))))
-(defun org-src--source-buffer ()
- "Return source buffer edited by current buffer."
- (unless (org-src-edit-buffer-p) (error "Not in a source buffer"))
- (or (marker-buffer org-src--beg-marker)
- (error "No source buffer available for current editing session")))
-
-(defun org-src--get-lang-mode (lang)
- "Return major mode that should be used for LANG.
-LANG is a string, and the returned major mode is a symbol."
- (intern
- (concat
- (let ((l (or (cdr (assoc lang org-src-lang-modes)) lang)))
- (if (symbolp l) (symbol-name l) l))
- "-mode")))
-
(defun org-src--coordinates (pos beg end)
"Return coordinates of POS relatively to BEG and END.
POS, BEG and END are buffer positions. Return value is either
@@ -397,7 +425,7 @@ Assume point is in the corresponding edit buffer."
(if org-src--preserve-indentation 0
(+ (or org-src--block-indentation 0)
(if (memq org-src--source-type '(example-block src-block))
- org-edit-src-content-indentation
+ org-src--content-indentation
0))))
(use-tabs? (and (> org-src--tab-width 0) t))
(source-tab-width org-src--tab-width)
@@ -405,8 +433,8 @@ Assume point is in the corresponding edit buffer."
(write-back org-src--allow-write-back))
(with-temp-buffer
;; Reproduce indentation parameters from source buffer.
- (setq-local indent-tabs-mode use-tabs?)
- (when (> source-tab-width 0) (setq-local tab-width source-tab-width))
+ (setq indent-tabs-mode use-tabs?)
+ (when (> source-tab-width 0) (setq tab-width source-tab-width))
;; Apply WRITE-BACK function on edit buffer contents.
(insert (org-no-properties contents))
(goto-char (point-min))
@@ -441,7 +469,6 @@ 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."
- (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))
@@ -457,11 +484,12 @@ Leave point in edit buffer."
(with-current-buffer old-edit-buffer (org-src--remove-overlay))
(kill-buffer old-edit-buffer))
(let* ((org-mode-p (derived-mode-p 'org-mode))
+ (source-file-name (buffer-file-name (buffer-base-buffer)))
(source-tab-width (if indent-tabs-mode tab-width 0))
(type (org-element-type datum))
- (ind (org-with-wide-buffer
- (goto-char (org-element-property :begin datum))
- (org-get-indentation)))
+ (block-ind (org-with-point-at (org-element-property :begin datum)
+ (current-indentation)))
+ (content-ind org-edit-src-content-indentation)
(preserve-ind
(and (memq type '(example-block src-block))
(or (org-element-property :preserve-indent datum)
@@ -498,16 +526,18 @@ Leave point in edit buffer."
;; Transmit buffer-local variables for exit function. It must
;; be done after initializing major mode, as this operation
;; may reset them otherwise.
- (setq-local org-src--tab-width source-tab-width)
- (setq-local org-src--from-org-mode org-mode-p)
- (setq-local org-src--beg-marker beg)
- (setq-local org-src--end-marker end)
- (setq-local org-src--remote remote)
- (setq-local org-src--source-type type)
- (setq-local org-src--block-indentation ind)
- (setq-local org-src--preserve-indentation preserve-ind)
- (setq-local org-src--overlay overlay)
- (setq-local org-src--allow-write-back write-back)
+ (setq org-src--tab-width source-tab-width)
+ (setq org-src--from-org-mode org-mode-p)
+ (setq org-src--beg-marker beg)
+ (setq org-src--end-marker end)
+ (setq org-src--remote remote)
+ (setq org-src--source-type type)
+ (setq org-src--block-indentation block-ind)
+ (setq org-src--content-indentation content-ind)
+ (setq org-src--preserve-indentation preserve-ind)
+ (setq org-src--overlay overlay)
+ (setq org-src--allow-write-back write-back)
+ (setq org-src-source-file-name source-file-name)
;; Start minor mode.
(org-src-mode)
;; Move mark and point in edit buffer to the corresponding
@@ -536,7 +566,7 @@ Leave point in edit buffer."
"Fontify code block.
This function is called by emacs automatic fontification, as long
as `org-src-fontify-natively' is non-nil."
- (let ((lang-mode (org-src--get-lang-mode lang)))
+ (let ((lang-mode (org-src-get-lang-mode lang)))
(when (fboundp lang-mode)
(let ((string (buffer-substring-no-properties start end))
(modified (buffer-modified-p))
@@ -631,13 +661,12 @@ This minor mode is turned on in two situations:
See also `org-src-mode-hook'."
nil " OrgSrc" nil
(when org-edit-src-persistent-message
- (setq-local
- header-line-format
- (substitute-command-keys
- (if org-src--allow-write-back
- "Edit, then exit with `\\[org-edit-src-exit]' or abort with \
+ (setq header-line-format
+ (substitute-command-keys
+ (if org-src--allow-write-back
+ "Edit, then exit with `\\[org-edit-src-exit]' or abort with \
`\\[org-edit-src-abort]'"
- "Exit with `\\[org-edit-src-exit]' or abort with \
+ "Exit with `\\[org-edit-src-exit]' or abort with \
`\\[org-edit-src-abort]'"))))
;; Possibly activate various auto-save features (for the edit buffer
;; or the source buffer).
@@ -646,7 +675,8 @@ See also `org-src-mode-hook'."
(concat (make-temp-name "org-src-")
(format-time-string "-%Y-%d-%m")
".txt")))
- (unless (or org-src--auto-save-timer (zerop org-edit-src-auto-save-idle-delay))
+ (unless (or org-src--auto-save-timer
+ (= 0 org-edit-src-auto-save-idle-delay))
(setq org-src--auto-save-timer
(run-with-idle-timer
org-edit-src-auto-save-idle-delay t
@@ -663,15 +693,13 @@ See also `org-src-mode-hook'."
(setq org-src--auto-save-timer nil)))))))))
(defun org-src-mode-configure-edit-buffer ()
+ "Configure the src edit buffer."
(when (bound-and-true-p org-src--from-org-mode)
(add-hook 'kill-buffer-hook #'org-src--remove-overlay nil 'local)
(if (bound-and-true-p org-src--allow-write-back)
(progn
(setq buffer-offer-save t)
- (setq buffer-file-name
- (concat (buffer-file-name (marker-buffer org-src--beg-marker))
- "[" (buffer-name) "]"))
- (setq-local write-contents-functions '(org-edit-src-save)))
+ (setq write-contents-functions '(org-edit-src-save)))
(setq buffer-read-only t))))
(add-hook 'org-src-mode-hook #'org-src-mode-configure-edit-buffer)
@@ -732,6 +760,15 @@ Org-babel commands."
(org-src-do-at-code-block
(call-interactively (lookup-key org-babel-map key)))))
+(defun org-src-get-lang-mode (lang)
+ "Return major mode that should be used for LANG.
+LANG is a string, and the returned major mode is a symbol."
+ (intern
+ (concat
+ (let ((l (or (cdr (assoc lang org-src-lang-modes)) lang)))
+ (if (symbolp l) (symbol-name l) l))
+ "-mode")))
+
(defun org-src-edit-buffer-p (&optional buffer)
"Non-nil when current buffer is a source editing buffer.
If BUFFER is non-nil, test it instead."
@@ -740,11 +777,34 @@ If BUFFER is non-nil, test it instead."
(local-variable-p 'org-src--beg-marker buffer)
(local-variable-p 'org-src--end-marker buffer))))
+(defun org-src-source-buffer ()
+ "Return source buffer edited in current buffer.
+Raise an error when current buffer is not a source editing buffer."
+ (unless (org-src-edit-buffer-p) (error "Not in a source buffer"))
+ (or (marker-buffer org-src--beg-marker)
+ (error "No source buffer available for current editing session")))
+
+(defun org-src-source-type ()
+ "Return type of element edited in current buffer.
+Raise an error when current buffer is not a source editing buffer."
+ (unless (org-src-edit-buffer-p) (error "Not in a source buffer"))
+ org-src--source-type)
+
(defun org-src-switch-to-buffer (buffer context)
(pcase org-src-window-setup
(`current-window (pop-to-buffer-same-window buffer))
(`other-window
(switch-to-buffer-other-window buffer))
+ (`split-window-below
+ (if (eq context 'exit)
+ (delete-window)
+ (select-window (split-window-vertically)))
+ (pop-to-buffer-same-window buffer))
+ (`split-window-right
+ (if (eq context 'exit)
+ (delete-window)
+ (select-window (split-window-horizontally)))
+ (pop-to-buffer-same-window buffer))
(`other-frame
(pcase context
(`exit
@@ -900,7 +960,7 @@ the LaTeX environment in the Org mode buffer."
(org-src--edit-element
element
(org-src--construct-edit-buffer-name (buffer-name) "LaTeX environment")
- (org-src--get-lang-mode "latex")
+ (org-src-get-lang-mode "latex")
t)
t))
@@ -925,7 +985,7 @@ Throw an error when not at an export block."
;; Missing export-block type. Fallback
;; to default mode.
"fundamental")))
- (mode (org-src--get-lang-mode type)))
+ (mode (org-src-get-lang-mode type)))
(unless (functionp mode) (error "No such language mode: %s" mode))
(org-src--edit-element
element
@@ -958,7 +1018,7 @@ name of the sub-editing buffer."
(let* ((lang
(if (eq type 'src-block) (org-element-property :language element)
"example"))
- (lang-f (and (eq type 'src-block) (org-src--get-lang-mode lang)))
+ (lang-f (and (eq type 'src-block) (org-src-get-lang-mode lang)))
(babel-info (and (eq type 'src-block)
(org-babel-get-src-block-info 'light)))
deactivate-mark)
@@ -977,7 +1037,7 @@ name of the sub-editing buffer."
(or (org-element-property :label-fmt element)
org-coderef-label-format))
(when (eq type 'src-block)
- (setq-local org-src--babel-info babel-info)
+ (setq org-src--babel-info babel-info)
(let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
(when (fboundp edit-prep-func)
(funcall edit-prep-func babel-info))))
@@ -991,7 +1051,7 @@ name of the sub-editing buffer."
(org-src--on-datum-p context))
(user-error "Not on inline source code"))
(let* ((lang (org-element-property :language context))
- (lang-f (org-src--get-lang-mode lang))
+ (lang-f (org-src-get-lang-mode lang))
(babel-info (org-babel-get-src-block-info 'light))
deactivate-mark)
(unless (functionp lang-f) (error "No such language mode: %s" lang-f))
@@ -1000,7 +1060,7 @@ name of the sub-editing buffer."
(org-src--construct-edit-buffer-name (buffer-name) lang)
lang-f
(lambda ()
- ;; Inline src blocks are limited to one line.
+ ;; Inline source blocks are limited to one line.
(while (re-search-forward "\n[ \t]*" nil t) (replace-match " "))
;; Trim contents.
(goto-char (point-min))
@@ -1010,8 +1070,8 @@ name of the sub-editing buffer."
(skip-chars-backward " \t")
(delete-region (point) (point-max))))
;; Finalize buffer.
- (setq-local org-src--babel-info babel-info)
- (setq-local org-src--preserve-indentation t)
+ (setq org-src--babel-info babel-info)
+ (setq org-src--preserve-indentation t)
(let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
(when (fboundp edit-prep-func) (funcall edit-prep-func babel-info)))
;; Return success.
@@ -1066,7 +1126,7 @@ Throw an error if there is no such buffer."
(beg org-src--beg-marker)
(end org-src--end-marker)
(overlay org-src--overlay))
- (with-current-buffer (org-src--source-buffer)
+ (with-current-buffer (org-src-source-buffer)
(undo-boundary)
(goto-char beg)
;; Temporarily disable read-only features of OVERLAY in order to
@@ -1122,10 +1182,7 @@ 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)
- (when org-src--saved-temp-window-config
- (set-window-configuration org-src--saved-temp-window-config)
- (setq org-src--saved-temp-window-config nil))))
+ (set-marker end nil)))
(provide 'org-src)