Bläddra i källkod

org-src: Allow to edit remotely inline src blocks

* lisp/org-src.el (org-edit-inline-src-code): New function.
(org-src--contents-area): Handle inline src blocks.

* lisp/org.el (org-edit-special): When on an inline src block, edit it.
Nicolas Goaziou 9 år sedan
förälder
incheckning
6c1f63b6a6
2 ändrade filer med 67 tillägg och 22 borttagningar
  1. 44 0
      lisp/org-src.el
  2. 23 22
      lisp/org.el

+ 44 - 0
lisp/org-src.el

@@ -292,6 +292,12 @@ where BEG and END are buffer positions and CONTENTS is a string."
 		     (search-forward "]")))
 	      (end (or (org-element-property :contents-end datum) beg)))
 	 (list beg end (buffer-substring-no-properties beg end))))
+      ((eq type 'inline-src-block)
+       (let ((beg (progn (goto-char (org-element-property :begin datum))
+			 (search-forward "{" (line-end-position) t)))
+	     (end (progn (goto-char (org-element-property :end datum))
+			 (search-backward "}" (line-beginning-position) t))))
+	 (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)))
@@ -871,6 +877,44 @@ name of the sub-editing buffer."
 	    (funcall edit-prep-func babel-info))))
       t)))
 
+(defun org-edit-inline-src-code ()
+  "Edit inline source code at point."
+  (interactive)
+  (let ((context (org-element-context)))
+    (unless (and (eq (org-element-type context) 'inline-src-block)
+		 (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))
+	   (babel-info (org-babel-get-src-block-info 'light))
+	   (ind (save-excursion
+		  (goto-char (org-element-property :begin context))
+		  (search-forward "{" (line-end-position) t)
+		  (current-column)))
+	   deactivate-mark)
+      (unless (functionp lang-f) (error "No such language mode: %s" lang-f))
+      (org-src--edit-element
+       context
+       (org-src--construct-edit-buffer-name (buffer-name) lang)
+       lang-f
+       (lambda ()
+	 ;; Inline src blocks are limited to one line.
+	 (while (re-search-forward "\n[ \t]*" nil t) (replace-match " "))
+	 ;; Trim contents.
+	 (goto-char (point-min))
+	 (skip-chars-forward " \t")
+	 (delete-region (point-min) (point))
+	 (goto-char (point-max))
+	 (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)
+      (let ((edit-prep-func (intern (concat "org-babel-edit-prep:" lang))))
+	(when (fboundp edit-prep-func) (funcall edit-prep-func babel-info)))
+      ;; Return success.
+      t)))
+
 (defun org-edit-fixed-width-region ()
   "Edit the fixed-width ASCII drawing at point.
 

+ 23 - 22
lisp/org.el

@@ -20987,21 +20987,21 @@ Otherwise, return a user error."
   (let ((element (org-element-at-point)))
     (assert (not buffer-read-only) nil
 	    "Buffer is read-only: %s" (buffer-name))
-    (case (org-element-type element)
-      (src-block
+    (pcase (org-element-type element)
+      (`src-block
        (if (not arg) (org-edit-src-code)
-         (let* ((info (org-babel-get-src-block-info))
-                (lang (nth 0 info))
-                (params (nth 2 info))
-                (session (cdr (assq :session params))))
-           (if (not session) (org-edit-src-code)
-             ;; At a src-block with a session and function called with
-             ;; an ARG: switch to the buffer related to the inferior
-             ;; process.
-             (switch-to-buffer
+	 (let* ((info (org-babel-get-src-block-info))
+		(lang (nth 0 info))
+		(params (nth 2 info))
+		(session (cdr (assq :session params))))
+	   (if (not session) (org-edit-src-code)
+	     ;; At a src-block with a session and function called with
+	     ;; an ARG: switch to the buffer related to the inferior
+	     ;; process.
+	     (switch-to-buffer
 	      (funcall (intern (concat "org-babel-prep-session:" lang))
 		       session params))))))
-      (keyword
+      (`keyword
        (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
            (org-open-link-from-string
 	    (format "[[%s]]"
@@ -21015,23 +21015,24 @@ Otherwise, return a user error."
 			      (match-string 0 value))
 			     (t (user-error "No valid file specified")))))))
          (user-error "No special environment to edit here")))
-      (table
+      (`table
        (if (eq (org-element-property :type element) 'table.el)
            (org-edit-table.el)
          (call-interactively 'org-table-edit-formulas)))
       ;; Only Org tables contain `table-row' type elements.
-      (table-row (call-interactively 'org-table-edit-formulas))
-      (example-block (org-edit-src-code))
-      (export-block (org-edit-export-block))
-      (fixed-width (org-edit-fixed-width-region))
-      (otherwise
+      (`table-row (call-interactively 'org-table-edit-formulas))
+      (`example-block (org-edit-src-code))
+      (`export-block (org-edit-export-block))
+      (`fixed-width (org-edit-fixed-width-region))
+      (_
        ;; No notable element at point.  Though, we may be at a link or
        ;; a footnote reference, which are objects.  Thus, scan deeper.
        (let ((context (org-element-context element)))
-	 (case (org-element-type context)
-	   (link (call-interactively #'ffap))
-	   (footnote-reference (org-edit-footnote-reference))
-	   (t (user-error "No special environment to edit here"))))))))
+	 (pcase (org-element-type context)
+	   (`footnote-reference (org-edit-footnote-reference))
+	   (`inline-src-block (org-edit-inline-src-code))
+	   (`link (call-interactively #'ffap))
+	   (_ (user-error "No special environment to edit here"))))))))
 
 (defvar org-table-coordinate-overlays) ; defined in org-table.el
 (defun org-ctrl-c-ctrl-c (&optional arg)