From 2e70cf7c7df61eebcc10e48077c5212d9015900e 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 | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 11 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 6ab13f4..3405ab4 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,65 @@ 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, such as #+CAPTION or #+LATEX_HEADER. + +If point is between the beginning of the line and the beginning +of the keyword, as denoted by \"#\", a keyword-line is inserted +above the current one. If the point is within the keyword, a new +keyword-line is inserted below. + +If point is in the middle of a keyword-line, after the keyword +itself, split the line depending on the value of +`org-M-RET-may-split-line'. See the docstring of this variable +for further details. + +Currently arg is ignored and the keyword is determined from the +context. + +The function is used by `org-meta-return'. Note, affiliated +keywords that are not allowed to appear multiple times are +ignored by `org-meta-return' (see +`org-element-affiliated-keywords' and +`org-element-multiple-keywords')." + (interactive "P") + (let* ((may-split (org-get-alist-option + org-M-RET-may-split-line 'keyword)) + (point-at-bol-p + (save-excursion (skip-chars-backward " \t") (bolp))) + (indention + (buffer-substring + (line-beginning-position) + (save-excursion + (beginning-of-line) + (skip-chars-forward " \t") + (point)))) + (keyword-re "#\\+\\(.+?\\):") + (key (save-excursion (beginning-of-line) + (skip-chars-forward " \t") + (looking-at keyword-re) + (upcase (org-match-string-no-properties 1)))) + (end-of-keyword (save-excursion + (beginning-of-line) + (re-search-forward keyword-re (line-end-position) t) + (point)))) + (when key + (when point-at-bol-p + (save-excursion + (beginning-of-line) + (insert "\n"))) + (unless may-split (end-of-line)) + (unless point-at-bol-p + (when (< (point) end-of-keyword) (end-of-line)) + (insert "\n")) + (insert (concat indention (format "#+%s: " key))) + (delete-region (point) (save-excursion (skip-chars-forward " \t") (point)))))) + (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 +21360,24 @@ 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 + (or (eq type 'keyword) + (< (point) (org-element-property :post-affiliated element))) + (let ((key (save-excursion + (beginning-of-line) + (skip-chars-forward " \t") + (and (looking-at "#\\+\\(.+?\\):") + (upcase (org-match-string-no-properties 1)))))) + (and key + (or (not (member key org-element-affiliated-keywords)) + (member key org-element-multiple-keywords))))) + (call-interactively 'org-insert-keyword)) + (t (call-interactively 'org-insert-heading)))))) ;;; Menu entries -- 2.1.3