[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

[PATCH] Add functionality for editing textareas in Org-mode



Hi List, 

here is patch for w3m-form.el that 

 - defines a boolean variable `w3m-form-textarea-use-org-mode-p' with
   default value nil
 - defines a function `w3m-form-toggle-textarea-major-mode' that toggles
   this variable
 - sets the major-mode in the w3m-form-input-textarea-buffer to
   text-mode or org-mode according to the value of
   `w3m-form-textarea-use-org-mode 
 - defrines two keymaps, one for org-mode and one for text-mode, since
   the default text-mode bindings clash with Org-mode bindings
 - use a full-sized buffer for Org-mode editing after applying
   `split-window-sensibly' to the current window (instead of a minimal
   buffer adapted to the content of the textarea).

Works fine so far except the mode conditional keymaps. I asked on the
emacs.help list how to do that, and received this advice:

,-----------------------------------------------------------------------
| You can define two keymaps (probably inheriting from a third one that
| contains the shared stuff), and then in the minor-mode function, setup
| minor-mode-overriding-map-alist to use the right one.
`-----------------------------------------------------------------------

but documentation for `minor-mode-overriding-map-alist' is so scarce
that I could not figure out by myself how to follow this advice -
unfortunately. Since I can only create Git patches, I attach the whole
modified w3m-format.el too, because it might be easier to simply ediff
the sources than to use this patch from CVS.
From 3ae6edaf9347a8e13cb583fe57621cc4fb7c7577 Mon Sep 17 00:00:00 2001
From: tj <tj@xxxxxxxxxxxxxx>
Date: Fri, 4 Oct 2013 19:51:35 +0200
Subject: [PATCH] Functionality for editing textareas in Org-mode added.

---
 w3m-form.el | 93 ++++++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 70 insertions(+), 23 deletions(-)

diff --git a/w3m-form.el b/w3m-form.el
index 57c67a4..6abd8e4 100644
--- a/w3m-form.el
+++ b/w3m-form.el
@@ -1121,16 +1121,28 @@ character."
 	    lines
 	    (get-text-property (point) 'w3m-form-readonly)))))
 
-(defvar w3m-form-input-textarea-map nil)
-(unless w3m-form-input-textarea-map
-  (setq w3m-form-input-textarea-map (make-sparse-keymap))
-  (define-key w3m-form-input-textarea-map "\C-c\C-c"
+(defvar w3m-form-input-textarea-default-map nil)
+(unless w3m-form-input-textarea-default-map
+  (setq w3m-form-input-textarea-default-map (make-sparse-keymap))
+  (define-key w3m-form-input-textarea-default-map "\C-c\C-c"
     'w3m-form-input-textarea-set)
-  (define-key w3m-form-input-textarea-map "\C-c\C-q"
+  (define-key w3m-form-input-textarea-default-map "\C-c\C-q"
     'w3m-form-input-textarea-exit)
-  (define-key w3m-form-input-textarea-map "\C-c\C-k"
+  (define-key w3m-form-input-textarea-default-map "\C-c\C-k"
     'w3m-form-input-textarea-exit)
-  (define-key w3m-form-input-textarea-map "\C-x\C-s"
+  (define-key w3m-form-input-textarea-default-map "\C-c\C-s"
+    'w3m-form-input-textarea-save))
+
+(defvar w3m-form-input-textarea-org-mode-map nil)
+(unless w3m-form-input-textarea-org-mode-map
+  (setq w3m-form-input-textarea-org-mode-map (make-sparse-keymap))
+  (define-key w3m-form-input-textarea-org-mode-map "\M-#c"
+    'w3m-form-input-textarea-set)
+  (define-key w3m-form-input-textarea-org-mode-map "\M-#q"
+    'w3m-form-input-textarea-exit)
+  (define-key w3m-form-input-textarea-org-mode-map "\M-#k"
+    'w3m-form-input-textarea-exit)
+  (define-key w3m-form-input-textarea-org-mode-map "\M-#s"
     'w3m-form-input-textarea-save))
 
 (defun w3m-form-input-textarea-filename (url id)
@@ -1249,25 +1261,58 @@ Minor mode to edit form textareas of w3m.
 		(not w3m-form-input-textarea-mode)))
     (run-hooks 'w3m-form-input-textarea-mode-hook)))
 
+
+(defvar w3m-form-textarea-use-org-mode-p nil
+  "Use org-mode for editing textareas when non-nil")
+
+(defun w3m-form-input-textarea-buffer-set-keymap ()
+  "Set keymap conditional on `w3m-form-textarea-use-org-mode-p'"
+  (if w3m-form-textarea-use-org-mode-p
+      (setq w3m-form-input-textarea-mode-map
+            w3m-form-input-textarea-org-mode-map)
+      (setq w3m-form-input-textarea-mode-map
+            w3m-form-input-textarea-default-map)))
+
+(defun w3m-form-toggle-textarea-major-mode (&optional arg)
+  "Toggle editing textareas in Org-mode.
+When off, textareas are edited in text-mode, otherwise in
+org-mode. With prefix argument ARG, use org-mode if ARG is
+positive, otherwise text-mode."
+  (interactive "P")
+  (setq w3m-form-textarea-use-org-mode-p
+        (if (null arg)
+            (not w3m-form-textarea-use-org-mode-p)
+          (> (prefix-numeric-value arg) 0)))
+  (w3m-form-input-textarea-buffer-set-keymap)
+  (message "Edit textarea in Org-mode %s"
+           (if w3m-form-textarea-use-org-mode-p "enabled" "disabled")))
+
+(add-hook 'w3m-form-input-textarea-mode-hook
+          (lambda ()
+            (and (eq major-mode 'org-mode))
+            (show-all)))
+
 (defvar view-mode-map)
 (defun w3m-form-input-textarea-mode-setup (caller-buffer readonly)
   (funcall (or (and readonly
 		    'view-mode)
-	       (and (functionp w3m-form-textarea-edit-mode)
-		    w3m-form-textarea-edit-mode)
-	       (when (buffer-live-p caller-buffer)
-		 (with-current-buffer caller-buffer
-		   (save-match-data
-		     (catch 'found-mode
-		       (dolist (elem w3m-form-textarea-edit-mode)
-			 (when (if (stringp (car elem))
-				   (string-match (car elem)
-						 w3m-current-url)
-				 (if (functionp (car elem))
-				     (funcall (car elem))
-				   (eval (car elem))))
-			   (throw 'found-mode (cdr elem))))))))
-	       'text-mode))
+               (and w3m-form-textarea-use-org-mode-p
+                    'org-mode)
+               (and (functionp w3m-form-textarea-edit-mode)
+                    w3m-form-textarea-edit-mode)
+               (when (buffer-live-p caller-buffer)
+                 (with-current-buffer caller-buffer
+                   (save-match-data
+                     (catch 'found-mode
+                       (dolist (elem w3m-form-textarea-edit-mode)
+                         (when (if (stringp (car elem))
+                                   (string-match (car elem)
+                                                 w3m-current-url)
+                                 (if (functionp (car elem))
+                                     (funcall (car elem))
+                                   (eval (car elem))))
+                           (throw 'found-mode (cdr elem))))))))
+               'text-mode))
   (if readonly
       (let ((keymap (copy-keymap view-mode-map)))
 	(set (make-local-variable 'minor-mode-map-alist)
@@ -1408,7 +1453,9 @@ selected rather than \(as usual\) some other window.  See
 						  (cdr buffer)
 						buffer)))
 	(condition-case nil
-	    (split-window cur-win (if (> size 0) size window-min-height))
+            (if w3m-form-textarea-use-org-mode-p
+                (split-window-sensibly cur-win)
+	    (split-window cur-win (if (> size 0) size window-min-height)))
 	  (error
 	   (delete-other-windows)
 	   (split-window cur-win (- (window-height cur-win)
-- 
1.8.2.3

Attachment: w3m-form.el
Description: Modified w3m-form.el


-- 
cheers,
Thorsten