From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Subject: [patch] extend org-meta-return to keywords Date: Sat, 22 Nov 2014 02:23:48 +0100 Message-ID: <87wq6o3u57.fsf@gmx.us> References: <87egszw8ui.fsf@gmx.us> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:59433) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XrzQs-0003j5-AU for emacs-orgmode@gnu.org; Fri, 21 Nov 2014 20:24:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XrzQl-0002rH-L2 for emacs-orgmode@gnu.org; Fri, 21 Nov 2014 20:24:14 -0500 Received: from plane.gmane.org ([80.91.229.3]:33633) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XrzQl-0002rB-AV for emacs-orgmode@gnu.org; Fri, 21 Nov 2014 20:24:07 -0500 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XrzQg-0001oa-3T for emacs-orgmode@gnu.org; Sat, 22 Nov 2014 02:24:02 +0100 Received: from 168.96.135.37.dynamic.jazztel.es ([37.135.96.168]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 22 Nov 2014 02:24:02 +0100 Received: from rasmus by 168.96.135.37.dynamic.jazztel.es with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 22 Nov 2014 02:24:02 +0100 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Hi, Rasmus writes: > Something I have wanted for a while is to have M-RET work "as > expected"(?) on keyword lines such as #+LATEX_HEADER, #+CAPTION etc. > [...] > Attached is a quick patch that works surprisingly well. I would work > more on it if you guys agree this would be useful. It probably need > much more work for corner-cases (any ideas what these are)? Attached is a new version of the patch that will respect the variables that also govern `org-insert-headline'. It's smarter and preserves the layout better. E.g. (where | is the cursor) #+CAP|TION: foo After M-RET it becomes #+CAPTION: foo #+CAPTION: | A keyword-line is inserted above when one press M-RET between BOL and "#". Keywords that an element can only have one of will not be inserted. E.g. #+NAME: foo| After M-RET it becomes #+NAME: foo * | Any opinions? —Rasmus -- I hear there's rumors on the, uh, Internets. . . --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-org.el-Add-keyword-support-to-M-RET.patch >From 1981d0b0a324565659629438ea253cc3e284e0bf Mon Sep 17 00:00:00 2001 From: rasmus Date: Wed, 19 Nov 2014 15:39:19 +0100 Subject: [PATCH] org.el: Add keyword-support to M-RET * org.el (org-insert-keyword): New function. (org-meta-return): May call `org-insert-keyword'. (org-M-RET-may-split-line): Add keyword. --- lisp/org.el | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 11 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 6ab13f4..e8e6b58 100755 --- a/lisp/org.el +++ b/lisp/org.el @@ -1601,6 +1601,7 @@ contexts. Valid contexts are: headline when creating a new headline item when creating a new item table in a table field +keyword when creating a new keyword default the value to be used for all contexts not explicitly customized" :group 'org-structure @@ -1614,6 +1615,7 @@ default the value to be used for all contexts not explicitly (const headline) (const item) (const table) + (const keyword) (const default)) (boolean))))) @@ -7725,8 +7727,8 @@ split the line and create a new headline with the text in the current line after point \(see `org-M-RET-may-split-line' on how to modify this behavior). -If point is at the beginning of a normal line, turn this line -into a heading. +If point is at the beginning of a normal line, excluding some +keywords, turn this line into a heading. When INVISIBLE-OK is set, stop at invisible headlines when going back. This is important for non-interactive uses of the @@ -21291,10 +21293,57 @@ number of stars to add." (forward-line))))))) (unless toggled (message "Cannot toggle heading from here")))) +(defun org-insert-keyword (&optional arg) + "Insert a new keyword-line. + +If point is between the beginning of the line and the start of +the keyword, a keyword-line is inserted above the current one. + +If point is in the middle of a keyword-line, split the line +depending on the value of `org-M-RET-may-split-line'. See the +docstring of this variable for further details. + +If point is within the keyword a new keyword-line is inserted +below. + +Currently arg is ignored and the keyword is determined from the +context. + +The function is used by `org-meta-return'. Note that keywords +that can only occur once per element are ignored (through +`org-meta-return')." + (interactive "P") + (let* ((may-split (org-get-alist-option + org-M-RET-may-split-line 'keyword)) + (point-at-bol-p (looking-back "^[[:space:]]*")) + (end-of-keyword + (save-excursion + (beginning-of-line) + (search-forward-regexp + org-element--affiliated-re (point-at-eol) t) + (point))) + (elm (org-element-at-point)) + (key (and (eq 'keyword (org-element-type elm)) + (org-element-property :key elm)))) + (when key + (when point-at-bol-p (open-line 1) + ;; Open-line makes sometimes ruins indention of the + ;; previous line. + (save-excursion (forward-line 1) + (org-indent-line))) + (unless may-split (end-of-line)) + (unless point-at-bol-p + (when (< (point) end-of-keyword) + (goto-char end-of-keyword)) + (insert "\n")) + (insert (format "#+%s: " key)) + (org-indent-line)))) + (defun org-meta-return (&optional arg) - "Insert a new heading or wrap a region in a table. -Calls `org-insert-heading' or `org-table-wrap-region', depending -on context. See the individual commands for more information." + "Insert a new heading, a new keyword or wrap a region in a table. +Calls `org-insert-heading', `org-insert-keyword' or +`org-table-wrap-region', depending on context. See the +individual commands for more information." (interactive "P") (org-check-before-invisible-edit 'insert) (or (run-hook-with-args-until-success 'org-metareturn-hook) @@ -21303,12 +21352,20 @@ on context. See the individual commands for more information." (when (eq type 'table-row) (setq element (org-element-property :parent element)) (setq type 'table)) - (if (and (eq type 'table) - (eq (org-element-property :type element) 'org) - (>= (point) (org-element-property :contents-begin element)) - (< (point) (org-element-property :contents-end element))) - (call-interactively 'org-table-wrap-region) - (call-interactively 'org-insert-heading))))) + (cond ((and (eq type 'table) + (eq (org-element-property :type element) 'org) + (>= (point) (org-element-property :contents-begin element)) + (< (point) (org-element-property :contents-end element))) + (call-interactively 'org-table-wrap-region)) + ((and (eq type 'keyword) + ;; Keyword such as LATEX, ATTR_LATEX, CAPTION, and HEADER, + ;; LATEX_HEADER, LATEX etc. can occur multiple times. + (let ((key (org-element-property :key element))) + (if (member key org-element-affiliated-keywords) + (member key org-element-multiple-keywords) + t))) + (call-interactively 'org-insert-keyword)) + (t (call-interactively 'org-insert-heading)))))) ;;; Menu entries -- 2.1.3 --=-=-=--