summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2016-01-06 14:43:37 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2016-01-06 14:45:52 +0100
commit22cf1bf7ed94fef38108c80b0330bfe00f664686 (patch)
tree351f6377286a9cd01fe93a5b5d476a79524ab238
parent3d4c4676737b3516adf9a12f03dd1ed7b67e3477 (diff)
downloadorg-mode-22cf1bf7ed94fef38108c80b0330bfe00f664686.tar.gz
Throw an error when trying to set invalid properties
* lisp/org.el (org--valid-property-p): New function. (org-entry-put): (org-set-property): Use new function. Suggested-by: Julien Cubizolles <j.cubizolles@free.fr> <http://permalink.gmane.org/gmane.emacs.orgmode/104044>
-rwxr-xr-xlisp/org.el43
-rw-r--r--testing/lisp/test-org.el7
2 files changed, 37 insertions, 13 deletions
diff --git a/lisp/org.el b/lisp/org.el
index b4ca27f..66f7a4e 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -15559,6 +15559,12 @@ but in some other way.")
"Some properties that are used by Org mode for various purposes.
Being in this list makes sure that they are offered for completion.")
+(defun org--valid-property-p (property)
+ "Non nil when string PROPERTY is a valid property name."
+ (not
+ (or (equal property "")
+ (org-string-match-p "\\s-" property))))
+
(defun org--update-property-plist (key val props)
"Associate KEY to VAL in alist PROPS.
Modifications are made by side-effect. Return new alist."
@@ -16079,8 +16085,9 @@ and the new value.")
(defun org-entry-put (pom property value)
"Set PROPERTY to VALUE for entry at point-or-marker POM.
-If the value is nil, it is converted to the empty string. If
-it is not a string, an error is raised.
+If the value is nil, it is converted to the empty string. If it
+is not a string, an error is raised. Also raise an error on
+invalid property names.
PROPERTY can be any regular property (see
`org-special-properties'). It can also be \"TODO\",
@@ -16090,7 +16097,9 @@ For the last two properties, VALUE may have any of the special
values \"earlier\" and \"later\". The function then increases or
decreases scheduled or deadline date by one day."
(cond ((null value) (setq value ""))
- ((not (stringp value)) (error "Properties values should be strings")))
+ ((not (stringp value)) (error "Properties values should be strings"))
+ ((not (org--valid-property-p property))
+ (user-error "Invalid property name: \"%s\"" property)))
(org-with-point-at pom
(if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
(org-back-to-heading t)
@@ -16373,21 +16382,29 @@ When use-default, don't even ask, just use the last
(defun org-set-property (property value)
"In the current entry, set PROPERTY to VALUE.
+
When called interactively, this will prompt for a property name, offering
completion on existing and default properties. And then it will prompt
for a value, offering completion either on allowed values (via an inherited
xxx_ALL property) or on existing values in other instances of this property
-in the current file."
+in the current file.
+
+Throw an error when trying to set a property with an invalid name."
(interactive (list nil nil))
- (let* ((property (or property (org-read-property-name)))
- (value (or value (org-read-property-value property)))
- (fn (cdr (assoc property org-properties-postprocess-alist))))
- (setq org-last-set-property property)
- (setq org-last-set-property-value (concat property ": " value))
- ;; Possibly postprocess the inserted value:
- (when fn (setq value (funcall fn value)))
- (unless (equal (org-entry-get nil property) value)
- (org-entry-put nil property value))))
+ (let ((property (or property (org-read-property-name))))
+ ;; `org-entry-put' also makes the following check, but this one
+ ;; avoids polluting `org-last-set-property' and
+ ;; `org-last-set-property-value' needlessly.
+ (unless (org--valid-property-p)
+ (user-error "Invalid property name: \"%s\"" property))
+ (let ((value (or value (org-read-property-value property)))
+ (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
+ (setq org-last-set-property property)
+ (setq org-last-set-property-value (concat property ": " value))
+ ;; Possibly postprocess the inserted value:
+ (when fn (setq value (funcall fn value)))
+ (unless (equal (org-entry-get nil property) value)
+ (org-entry-put nil property value)))))
(defun org-find-property (property &optional value)
"Find first entry in buffer that sets PROPERTY.
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index b4dff70..77fbd93 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -3587,6 +3587,13 @@ Paragraph<point>"
(should-error
(org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
(org-entry-put 1 "test" 2)))
+ ;; Error when property name is invalid.
+ (should-error
+ (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
+ (org-entry-put 1 "no space" "value")))
+ (should-error
+ (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
+ (org-entry-put 1 "" "value")))
;; Set "TODO" property.
(should
(string-match (regexp-quote " TODO H")