Browse Source

String handling improvements.

* lisp/org-agenda.el (org-agenda-export-csv-mapper): Use `replace-regexp-in-string'.
* lisp/org-capture.el (org-capture): Use `string-prefix-p'.
* lisp/org-compat.el (string-blank-p, string-remove-prefix):
* lisp/org-compat.el (string-remove-suffix): Add compatibility defuns
for emacs 24.3.
* lisp/org-datetree.el (org-datetree-cleanup): Use `string-suffix-p'.
* lisp/org-footnote.el (org-footnote-normalize-label): Use
`string-remove-prefix'.
* lisp/org-gnus.el (org-gnus-group-link): Use `string-prefix-p'.
* lisp/org-list.el (org-list-struct):
(org-list-get-list-type, org-list-send-item):
(org-list-use-alpha-bul-p): Use `string-match-p'.
* lisp/org.el (org-paste-subtree): Use `string-suffix-p'.
(org-open-file, org-refile-get-targets): Use `replace-regexp-in-string'.
(org-make-tags-matcher): Use `string-prefix-p'.
(org-set-tags): Use `string-(prefix,suffix)-p'.
* lisp/ox-beamer.el (org-beamer--element-has-overlay-p):
(org-beamer--format-block):
Use `string-(prefix,suffix)-p'.
* lisp/ox-html.el (org-html-fix-class-name): Use `replace-regexp-in-string'.
* lisp/ox-md.el (org-md-verbatim): Use `string-(prefix,suffix)-p'.
(org-md-paragraph): Use `string-prefix-p'.
Aaron Ecay 2 years ago
parent
commit
1c18efb615
11 changed files with 46 additions and 38 deletions
  1. 1 3
      lisp/org-agenda.el
  2. 1 1
      lisp/org-capture.el
  3. 17 1
      lisp/org-compat.el
  4. 1 1
      lisp/org-datetree.el
  5. 2 4
      lisp/org-footnote.el
  6. 1 1
      lisp/org-gnus.el
  7. 4 4
      lisp/org-list.el
  8. 10 13
      lisp/org.el
  9. 4 2
      lisp/ox-beamer.el
  10. 1 4
      lisp/ox-html.el
  11. 4 4
      lisp/ox-md.el

+ 1 - 3
lisp/org-agenda.el

@@ -3255,9 +3255,7 @@ This ensures the export commands can easily use it."
 	   ((not res) "")
 	   ((stringp res) res)
 	   (t (prin1-to-string res))))
-    (while (string-match "," res)
-      (setq res (replace-match ";" t t res)))
-    (org-trim res)))
+    (org-trim (replace-regexp-in-string "," ";" res nil t))))
 
 ;;;###autoload
 (defun org-store-agenda-views (&rest parameters)

+ 1 - 1
lisp/org-capture.el

@@ -628,7 +628,7 @@ of the day at point (if any) or the current HH:MM time."
 	       (equal (car (org-capture-get :target)) 'function))
 	    ((error quit)
 	     (if (and (buffer-base-buffer (current-buffer))
-		      (string-match "\\`CAPTURE-" (buffer-name)))
+		      (string-prefix-p "CAPTURE-" (buffer-name)))
 		 (kill-buffer (current-buffer)))
 	     (set-window-configuration (org-capture-get :return-to-wconf))
 	     (error "Capture template `%s': %s"

+ 17 - 1
lisp/org-compat.el

@@ -430,7 +430,23 @@ attention to case differences."
     (let ((start-pos (- (length string) (length suffix))))
       (and (>= start-pos 0)
 	   (eq t (compare-strings suffix nil nil
-				  string start-pos nil ignore-case))))))
+				  string start-pos nil ignore-case)))))
+
+  (defsubst string-blank-p (string)
+    "Check whether STRING is either empty or only whitespace."
+    (string-match-p "\\`[ \t\n\r]*\\'" string))
+
+  (defsubst string-remove-prefix (prefix string)
+    "Remove PREFIX from STRING if present."
+    (if (string-prefix-p prefix string)
+	(substring string (length prefix))
+      string))
+
+  (defsubst string-remove-suffix (suffix string)
+    "Remove SUFFIX from STRING if present."
+    (if (string-suffix-p suffix string)
+	(substring string 0 (- (length string) (length suffix)))
+      string)))
 
 (provide 'org-compat)
 

+ 1 - 1
lisp/org-datetree.el

@@ -192,7 +192,7 @@ before running this command, even though the command tries to be smart."
 		    (max (line-beginning-position)
 			 (- (match-beginning 0) org-ds-keyword-length))
 		    (match-beginning 0))))
-	  (when (or (string-match "-\\'" tmp)
+	  (when (or (string-suffix-p "-" tmp)
 		    (string-match dre tmp)
 		    (string-match sre tmp))
 	    (throw 'next nil))

+ 2 - 4
lisp/org-footnote.el

@@ -563,10 +563,8 @@ value if point was successfully moved."
 If LABEL is the empty string or constituted of white spaces only,
 return nil instead."
   (let ((label (org-trim label)))
-    (cond
-     ((equal "" label) nil)
-     ((string-match "\\`fn:" label) (replace-match "" nil nil label))
-     (t label))))
+    (unless (equal "" label)
+      (string-remove-prefix "fn:" label))))
 
 (defun org-footnote-get-definition (label)
   "Return label, boundaries and definition of the footnote LABEL."

+ 1 - 1
lisp/org-gnus.el

@@ -107,7 +107,7 @@ Otherwise create a link to the group inside Gnus.
 If `org-store-link' was called with a prefix arg the meaning of
 `org-gnus-prefer-web-links' is reversed."
   (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
-    (if (and (string-match "^nntp" group) ;; Only for nntp groups
+    (if (and (string-prefix-p "nntp" group) ;; Only for nntp groups
 	     (org-xor current-prefix-arg
 		      org-gnus-prefer-web-links))
 	(concat (if (string-match "gmane" unprefixed-group)

+ 4 - 4
lisp/org-list.el

@@ -658,7 +658,7 @@ Assume point is at an item."
 		       (match-string-no-properties 2) ; counter
 		       (match-string-no-properties 3) ; checkbox
 		       ;; Description tag.
-		       (and (save-match-data (string-match "[-+*]" bullet))
+		       (and (string-match-p "[-+*]" bullet)
 			    (match-string-no-properties 4)))))))
 	   (end-before-blank
 	    (function
@@ -1025,7 +1025,7 @@ Possible types are `descriptive', `ordered' and `unordered'.  The
 type is determined by the first item of the list."
   (let ((first (org-list-get-list-begin item struct prevs)))
     (cond
-     ((string-match "[[:alnum:]]" (org-list-get-bullet first struct)) 'ordered)
+     ((string-match-p "[[:alnum:]]" (org-list-get-bullet first struct)) 'ordered)
      ((org-list-get-tag first struct) 'descriptive)
      (t 'unordered))))
 
@@ -1471,7 +1471,7 @@ This function returns, destructively, the new list structure."
 		      (save-excursion
 			(goto-char (org-list-get-last-item item struct prevs))
 			(point-at-eol)))
-		     ((string-match "\\`[0-9]+\\'" dest)
+		     ((string-match-p "\\`[0-9]+\\'" dest)
 		      (let* ((all (org-list-get-all-items item struct prevs))
 			     (len (length all))
 			     (index (mod (string-to-number dest) len)))
@@ -1645,7 +1645,7 @@ as returned by `org-list-prevs-alist'."
 	   (while item
 	     (let ((count (org-list-get-counter item struct)))
 	       ;; Virtually determine current bullet
-	       (if (and count (string-match "[a-zA-Z]" count))
+	       (if (and count (string-match-p "[a-zA-Z]" count))
 		   ;; Counters are not case-sensitive.
 		   (setq ascii (string-to-char (upcase count)))
 		 (setq ascii (1+ ascii)))

+ 10 - 13
lisp/org.el

@@ -8803,7 +8803,7 @@ When REMOVE is non-nil, remove the subtree from the clipboard."
      (setq beg (point))
      (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
      (insert-before-markers txt)
-     (unless (string-match "\n\\'" txt) (insert "\n"))
+     (unless (string-suffix-p "\n" txt) (insert "\n"))
      (setq newend (point))
      (org-reinstall-markers-in-region beg)
      (setq end (point))
@@ -11486,12 +11486,11 @@ If the file does not exist, an error is thrown."
       ;; Remove quotes around the file name - we'll use shell-quote-argument.
       (while (string-match "['\"]%s['\"]" cmd)
 	(setq cmd (replace-match "%s" t t cmd)))
-      (while (string-match "%s" cmd)
-	(setq cmd (replace-match
-		   (save-match-data
-		     (shell-quote-argument
-		      (convert-standard-filename file)))
-		   t t cmd)))
+      (setq cmd (replace-regexp-in-string
+		 "%s"
+		 (shell-quote-argument (convert-standard-filename file))
+		 cmd
+		 nil t))
 
       ;; Replace "%1", "%2" etc. in command with group matches from regex
       (save-match-data
@@ -11780,9 +11779,7 @@ order.")
     (nreverse targets)))
 
 (defun org-protect-slash (s)
-  (while (string-match "/" s)
-    (setq s (replace-match "\\" t t s)))
-  s)
+  (replace-regexp-in-string "/" "\\/" s nil t))
 
 (defun org--get-outline-path-1 (&optional use-cache)
   "Return outline path to current headline.
@@ -14590,7 +14587,7 @@ See also `org-scan-tags'."
 	(progn
 	  (setq tagsmatch (substring match 0 (match-beginning 0)))
 	  (setq todomatch (substring match (match-end 0)))
-	  (when (string-match "\\`!" todomatch)
+	  (when (string-prefix-p "!" todomatch)
 	    (setq org--matcher-tags-todo-only t)
 	    (setq todomatch (substring todomatch 1)))
 	  (when (string-match "\\`\\s-*\\'" todomatch)
@@ -15143,8 +15140,8 @@ When JUST-ALIGN is non-nil, only align tags."
 		   ":")))
 
 	  (if (not (org-string-nw-p tags)) (setq tags "")
-	    (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
-	    (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
+	    (unless (string-suffix-p ":" tags) (setq tags (concat tags ":")))
+	    (unless (string-prefix-p ":" tags) (setq tags (concat ":" tags))))
 
 	  ;; Insert new tags at the correct column
 	  (beginning-of-line)

+ 4 - 2
lisp/ox-beamer.el

@@ -219,7 +219,8 @@ Return overlay specification, as a string, or nil."
   (let ((first-object (car (org-element-contents element))))
     (when (eq (org-element-type first-object) 'export-snippet)
       (let ((value (org-element-property :value first-object)))
-	(and (string-match "\\`<.*>\\'" value) value)))))
+	(and (string-prefix-p "<" value) (string-suffix-p ">" value)
+	     value)))))
 
 
 
@@ -558,7 +559,8 @@ used as a communication channel."
 	  (let ((action (org-element-property :BEAMER_ACT headline)))
 	    (cond
 	     ((not action) (list (cons "a" "") (cons "A" "") (cons "R" "")))
-	     ((string-match "\\`\\[.*\\]\\'" action)
+	     ((and (string-prefix-p "[" action)
+		   (string-suffix-p "]" action))
 	      (list
 	       (cons "A" (org-beamer--normalize-argument action 'defaction))
 	       (cons "a" "")

+ 1 - 4
lisp/ox-html.el

@@ -1725,10 +1725,7 @@ produce code that uses these same face definitions."
 (defun org-html-fix-class-name (kwd)	; audit callers of this function
   "Turn todo keyword KWD into a valid class name.
 Replaces invalid characters with \"_\"."
-  (save-match-data
-    (while (string-match "[^a-zA-Z0-9_]" kwd)
-      (setq kwd (replace-match "_" t t kwd))))
-  kwd)
+  (replace-regexp-in-string "[^a-zA-Z0-9_]" "_" kwd nil t))
 
 (defun org-html-footnote-section (info)
   "Format the footnote section.

+ 4 - 4
lisp/ox-md.el

@@ -168,8 +168,8 @@ CONTENTS is nil.  INFO is a plist used as a communication
 channel."
   (let ((value (org-element-property :value verbatim)))
     (format (cond ((not (string-match "`" value)) "`%s`")
-		  ((or (string-match "\\``" value)
-		       (string-match "`\\'" value))
+		  ((or (string-prefix-p "`" value)
+		       (string-suffix-p "`" value))
 		   "`` %s ``")
 		  (t "``%s``"))
 	    value)))
@@ -426,8 +426,8 @@ CONTENTS is the paragraph contents.  INFO is a plist used as
 a communication channel."
   (let ((first-object (car (org-element-contents paragraph))))
     ;; If paragraph starts with a #, protect it.
-    (if (and (stringp first-object) (string-match "\\`#" first-object))
-	(replace-regexp-in-string "\\`#" "\\#" contents nil t)
+    (if (and (stringp first-object) (string-prefix-p "#" first-object))
+	(concat "\\" contents)
       contents)))