Browse Source

Merge branch 'maint'

Bastien Guerry 12 years ago
parent
commit
db51b80d2b
2 changed files with 62 additions and 50 deletions
  1. 33 35
      lisp/org-src.el
  2. 29 15
      lisp/org.el

+ 33 - 35
lisp/org-src.el

@@ -201,41 +201,39 @@ There is a mode hook, and keybindings for `org-edit-src-exit' and
 `org-edit-src-save'")
 
 (defun org-edit-src-code (&optional context code edit-buffer-name)
-  "Edit the source CODE example at point.
-The example is copied to a separate buffer, and that buffer is
-switched to the correct language mode.  When done, exit with
-\\[org-edit-src-exit].  This will remove the original code in the
-Org buffer, and replace it with the edited version.  An optional
-argument CONTEXT is used by \\[org-edit-src-save] when calling
-this function.  See `org-src-window-setup' to configure the
-display of windows containing the Org buffer and the code
-buffer."
+  "Edit the source CODE block at point.
+The code is copied to a separate buffer and the appropriate mode
+is turned on.  When done, exit with \\[org-edit-src-exit].  This will
+remove the original code in the Org buffer, and replace it with the
+edited version.  An optional argument CONTEXT is used by \\[org-edit-src-save]
+when calling this function.  See `org-src-window-setup' to configure
+the display of windows containing the Org buffer and the code buffer."
   (interactive)
-  (unless (eq context 'save)
-    (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
-  (let* ((mark (and (org-region-active-p) (mark)))
-	 (case-fold-search t)
-	 (info
-	  ;; If the src region consists in no lines, we insert a blank
-	  ;; line.
-	  (let* ((temp (org-edit-src-find-region-and-lang))
-		 (beg (nth 0 temp))
-		 (end (nth 1 temp)))
-	    (if (>= end beg) temp
-	      (goto-char beg)
-	      (insert "\n")
-	      (org-edit-src-find-region-and-lang))))
-	 (full-info (org-babel-get-src-block-info 'light))
-	 (org-mode-p (derived-mode-p 'org-mode)) ;; derived-mode-p is reflexive
-	 (beg (make-marker))
-	 ;; Move marker with inserted text for case when src block is
-	 ;; just one empty line, i.e. beg == end.
-	 (end (copy-marker (make-marker) t))
-	 (allow-write-back-p (null code))
-	 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
-	 begline markline markcol line col transmitted-variables)
-    (if (not info)
-	nil
+  (if (not (org-in-src-block-p))
+      (user-error "Not in a source code block")
+    (unless (eq context 'save)
+      (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
+    (let* ((mark (and (org-region-active-p) (mark)))
+	   (case-fold-search t)
+	   (info
+	    ;; If the src region consists in no lines, we insert a blank
+	    ;; line.
+	    (let* ((temp (org-edit-src-find-region-and-lang))
+		   (beg (nth 0 temp))
+		   (end (nth 1 temp)))
+	      (if (>= end beg) temp
+		(goto-char beg)
+		(insert "\n")
+		(org-edit-src-find-region-and-lang))))
+	   (full-info (org-babel-get-src-block-info 'light))
+	   (org-mode-p (derived-mode-p 'org-mode)) ;; derived-mode-p is reflexive
+	   (beg (make-marker))
+	   ;; Move marker with inserted text for case when src block is
+	   ;; just one empty line, i.e. beg == end.
+	   (end (copy-marker (make-marker) t))
+	   (allow-write-back-p (null code))
+	   block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
+	   begline markline markcol line col transmitted-variables)
       (setq beg (move-marker beg (nth 0 info))
 	    end (move-marker end (nth 1 info))
 	    msg (if allow-write-back-p
@@ -244,7 +242,7 @@ buffer."
 		  "Exit with C-c ' (C-c and single quote)")
 	    code (or code (buffer-substring-no-properties beg end))
 	    lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
-                     (nth 2 info))
+		     (nth 2 info))
 	    lang (if (symbolp lang) (symbol-name lang) lang)
 	    single (nth 3 info)
 	    block-nindent (nth 5 info)

+ 29 - 15
lisp/org.el

@@ -19300,12 +19300,13 @@ See the individual commands for more information."
 (defun org-edit-special (&optional arg)
   "Call a special editor for the stuff at point.
 When at a table, call the formula editor with `org-table-edit-formulas'.
-When at the first line of an src example, call `org-edit-src-code'.
-When in an #+include line, visit the include file.  Otherwise call
-`ffap' to visit the file at point."
+When in a source code block, call `org-edit-src-code'.
+When in an #+include line, visit the included file.
+On a link, call `ffap' to visit the link at point.
+Otherwise, return a user error."
   (interactive)
   ;; possibly prep session before editing source
-  (when arg
+  (when (and (org-in-src-block-p) arg)
     (let* ((info (org-babel-get-src-block-info))
            (lang (nth 0 info))
            (params (nth 2 info))
@@ -19318,16 +19319,15 @@ When in an #+include line, visit the include file.  Otherwise call
       (beginning-of-line 1)
       (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
     (find-file (org-trim (match-string 1))))
-   ((org-edit-src-code))
-   ((org-edit-fixed-width-region))
-   ((org-at-table.el-p)
-    (org-edit-src-code))
    ((or (org-at-table-p)
 	(save-excursion
 	  (beginning-of-line 1)
 	  (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
     (call-interactively 'org-table-edit-formulas))
-   (t (call-interactively 'ffap))))
+   ((or (org-in-src-block-p) (org-at-table.el-p)) (org-edit-src-code))
+   ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
+   ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
+   (t (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)
@@ -20589,13 +20589,27 @@ and end of string."
   "Is S an ID created by UUIDGEN?"
   (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
 
-(defun org-in-src-block-p nil
-  "Whether point is in a code source block."
+(defun org-in-src-block-p (&optional inside)
+  "Whether point is in a code source block.
+When INSIDE is non-nil, don't consider we are within a src block
+when point is at #+BEGIN_SRC or #+END_SRC."
   (let (ov)
-    (when (setq ov (overlays-at (point)))
-      (memq 'org-block-background
-	    (overlay-properties
-	     (car ov))))))
+    (or (when (setq ov (overlays-at (point)))
+	  (memq 'org-block-background
+		(overlay-properties
+		 (car ov))))
+	(and (not inside)
+	     (save-match-data
+	       (save-excursion
+		 (move-beginning-of-line 1)
+		 (looking-at ".*#\\+\\(BEGIN\\|END\\)_SRC")))))))
+
+(defun org-in-fixed-width-region-p ()
+  "Whether point is in a fixed-width region."
+  (save-match-data
+    (save-excursion
+      (beginning-of-line 1)
+      (looking-at "^: "))))
 
 (defun org-context ()
   "Return a list of contexts of the current cursor position.