Browse Source

Clean up string handling.

* lisp/org-compat.el (string-prefix-p, string-suffix-p):
Add compatibility definitions for 24.3.

* lisp/ob-R.el (org-babel-edit-prep:R):
* lisp/ob-core.el (org-babel-demarcate-block):
* lisp/ob-js.el (org-babel-js-read):
* lisp/ob-latex.el (org-babel-execute:latex):
* lisp/ob-ref.el (org-babel-ref-parse):
* lisp/ob-shell.el (org-babel-variable-assignments:shell):
* lisp/org-protocol.el (org-protocol-create):
* lisp/org-table.el (org-table-field-info):
* lisp/org.el (org-get-buffer-for-internal-link):
* lisp/ox-publish.el (org-publish-compare-directory-files):
* lisp/ox-texinfo.el (org-texinfo-template):
Use `string-{suffix,prefix}-p' instead of `string-match(-p)'.

* lisp/ob-python.el (org-babel-python-read-string):
Ditto, also use `substring' rather than `match-string'.

* lisp/org-table.el (org-table-copy-down):
(org-table-insert-hline, org-table-current-field-formula):
(org-table-get-formula): Use `string-match-p' instead of
`string-match'.
Aaron Ecay 3 years ago
parent
commit
6b52bc6a21
13 changed files with 59 additions and 29 deletions
  1. 4 2
      lisp/ob-R.el
  2. 2 1
      lisp/ob-core.el
  3. 3 1
      lisp/ob-js.el
  4. 7 7
      lisp/ob-latex.el
  5. 3 2
      lisp/ob-python.el
  6. 2 1
      lisp/ob-ref.el
  7. 1 1
      lisp/ob-shell.el
  8. 21 1
      lisp/org-compat.el
  9. 4 4
      lisp/org-protocol.el
  10. 8 5
      lisp/org-table.el
  11. 1 1
      lisp/org.el
  12. 2 2
      lisp/ox-publish.el
  13. 1 1
      lisp/ox-texinfo.el

+ 4 - 2
lisp/ob-R.el

@@ -91,8 +91,10 @@ this variable.")
 (defvar ess-local-process-name)   ; dynamically scoped
 (defun org-babel-edit-prep:R (info)
   (let ((session (cdr (assoc :session (nth 2 info)))))
-    (when (and session (string-match "^\\*\\(.+?\\)\\*$" session))
-      (save-match-data (org-babel-R-initiate-session session nil)))))
+    (when (and session
+	       (string-prefix-p "*"  session)
+	       (string-suffix-p "*" session))
+      (org-babel-R-initiate-session session nil))))
 
 ;; The usage of utils::read.table() ensures that the command
 ;; read.table() can be found even in circumstances when the utils

+ 2 - 1
lisp/ob-core.el

@@ -1844,7 +1844,8 @@ region is not active then the point is demarcated."
 			lang "\n"
 			body
 			(if (or (= (length body) 0)
-				(string-match "[\r\n]$" body)) "" "\n")
+				(string-suffix-p "\r" body)
+				(string-suffix-p "\n" body)) "" "\n")
 			(funcall (if lower-case-p 'downcase 'upcase) "#+end_src\n")))
 	(goto-char start) (move-end-of-line 1)))))
 

+ 3 - 1
lisp/ob-js.el

@@ -96,7 +96,9 @@ This function is called by `org-babel-execute-src-block'"
 If RESULTS look like a table, then convert them into an
 Emacs-lisp table, otherwise return the results as a string."
   (org-babel-read
-   (if (and (stringp results) (string-match "^\\[[^\000]+\\]$" results))
+   (if (and (stringp results)
+	    (string-prefix-p "[" results)
+	    (string-suffix-p "]" results))
        (org-babel-read
         (concat "'"
                 (replace-regexp-in-string

+ 7 - 7
lisp/ob-latex.el

@@ -106,15 +106,15 @@ This function is called by `org-babel-execute-src-block'."
 	     (org-latex-packages-alist
 	      (append (cdr (assoc :packages params)) org-latex-packages-alist)))
         (cond
-         ((and (string-match "\\.png$" out-file) (not imagemagick))
+         ((and (string-suffix-p ".png" out-file) (not imagemagick))
           (org-create-formula-image
            body out-file org-format-latex-options in-buffer))
-         ((string-match "\\.tikz$" out-file)
+         ((string-suffix-p ".tikz" out-file)
 	  (when (file-exists-p out-file) (delete-file out-file))
 	  (with-temp-file out-file
 	    (insert body)))
-	 ((and (or (string-match "\\.svg$" out-file)
-		   (string-match "\\.html$" out-file))
+	 ((and (or (string-suffix-p ".svg" out-file)
+		   (string-suffix-p ".html" out-file))
 	       (executable-find org-babel-latex-htlatex))
 	  ;; TODO: this is a very different way of generating the
 	  ;; frame latex document than in the pdf case.  Ideally, both
@@ -144,7 +144,7 @@ This function is called by `org-babel-execute-src-block'."
 	    (shell-command (format "%s %s" org-babel-latex-htlatex tex-file)))
 	  (cond
 	   ((file-exists-p (concat (file-name-sans-extension tex-file) "-1.svg"))
-	    (if (string-match "\\.svg$" out-file)
+	    (if (string-suffix-p ".svg" out-file)
 		(progn
 		  (shell-command "pwd")
 		  (shell-command (format "mv %s %s"
@@ -152,13 +152,13 @@ This function is called by `org-babel-execute-src-block'."
 					 out-file)))
 	      (error "SVG file produced but HTML file requested")))
 	   ((file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
-	    (if (string-match "\\.html$" out-file)
+	    (if (string-suffix-p ".html" out-file)
 		(shell-command "mv %s %s"
 			       (concat (file-name-sans-extension tex-file)
 				       ".html")
 			       out-file)
 	      (error "HTML file produced but SVG file requested")))))
-	 ((or (string-match "\\.pdf$" out-file) imagemagick)
+	 ((or (string-suffix-p ".pdf" out-file) imagemagick)
 	  (with-temp-file tex-file
 	    (require 'ox-latex)
 	    (insert

+ 3 - 2
lisp/ob-python.el

@@ -334,8 +334,9 @@ last statement in BODY, as elisp."
 
 (defun org-babel-python-read-string (string)
   "Strip \\='s from around Python string."
-  (if (string-match "^'\\([^\000]+\\)'$" string)
-      (match-string 1 string)
+  (if (and (string-prefix-p "'" string)
+	   (string-suffix-p "'" string))
+      (substring string 1 -1)
     string))
 
 (provide 'ob-python)

+ 2 - 1
lisp/ob-ref.el

@@ -91,7 +91,8 @@ the variable."
 					org-babel-current-src-block-location)))
 			 (org-babel-read ref))))
 	      (if (equal out ref)
-		  (if (string-match "^\".*\"$" ref)
+		  (if (and (string-prefix-p "\"" ref)
+			   (string-suffix-p "\"" ref))
 		      (read ref)
 		    (org-babel-ref-resolve ref))
 		out))))))

+ 1 - 1
lisp/ob-shell.el

@@ -148,7 +148,7 @@ This function is called by `org-babel-execute-src-block'."
 		     "hline"))))
     (mapcar
      (lambda (pair)
-       (if (string-match "bash$" shell-file-name)
+       (if (string-suffix-p "bash" shell-file-name)
 	   (org-babel-variable-assignments:bash
             (car pair) (cdr pair) sep hline)
          (org-babel-variable-assignments:sh-generic

+ 21 - 1
lisp/org-compat.el

@@ -402,7 +402,7 @@ effect, which variables to use depends on the Emacs version."
     `(org-unmodified ,@body)))
 (def-edebug-spec org-with-silent-modifications (body))
 
-;; Remove this when support for Emacs < 24.4 is dropped.
+;; Functions for Emacs < 24.4 compatibility
 (defun org-define-error (name message)
   "Define NAME as a new error signal.
 MESSAGE is a string that will be output to the echo area if such
@@ -412,6 +412,26 @@ Implements `define-error' for older emacsen."
     (put name 'error-conditions
 	 (copy-sequence (cons name (get 'error 'error-conditions))))))
 
+(unless (fboundp 'string-prefix-p)
+  ;; From Emacs subr.el.
+  (defun string-prefix-p (prefix string &optional ignore-case)
+    "Return non-nil if PREFIX is a prefix of STRING.
+If IGNORE-CASE is non-nil, the comparison is done without paying attention
+to case differences."
+    (let ((prefix-length (length prefix)))
+      (if (> prefix-length (length string)) nil
+	(eq t (compare-strings prefix 0 prefix-length string
+			       0 prefix-length ignore-case)))))
+
+  (defun string-suffix-p (suffix string  &optional ignore-case)
+    "Return non-nil if SUFFIX is a suffix of STRING.
+If IGNORE-CASE is non-nil, the comparison is done without paying
+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))))))
+
 (provide 'org-compat)
 
 ;;; org-compat.el ends here

+ 4 - 4
lisp/org-protocol.el

@@ -678,14 +678,14 @@ the cdr of an element in `org-publish-project-alist', reuse
         (minibuffer-allow-text-properties nil))
 
     (setq base-url (read-string "Base URL of published content: " base-url nil base-url t))
-    (if (not (string-match "\\/$" base-url))
-        (setq base-url (concat base-url "/")))
+    (or (string-suffix-p "/" base-url)
+	(setq base-url (concat base-url "/")))
 
     (setq working-dir
           (expand-file-name
            (read-directory-name "Local working directory: " working-dir working-dir t)))
-    (if (not (string-match "\\/$" working-dir))
-        (setq working-dir (concat working-dir "/")))
+    (or (string-suffix-p "/" working-dir)
+	(setq working-dir (concat working-dir "/")))
 
     (setq strip-suffix
           (read-string

+ 8 - 5
lisp/org-table.el

@@ -1163,7 +1163,7 @@ to a number.  In the case of a timestamp, increment by days."
 	(user-error "No non-empty field found")
       (if (and org-table-copy-increment
 	       (not (equal orig-n 0))
-	       (string-match "^[-+^/*0-9eE.]+$" txt)
+	       (string-match-p "^[-+^/*0-9eE.]+$" txt)
 	       (< (string-to-number txt) 100000000))
 	  (setq txt (calc-eval (concat txt "+" (number-to-string inc)))))
       (insert txt)
@@ -1306,7 +1306,10 @@ is always the old value."
 		   (concat ", formula: "
 			   (org-table-formula-to-user
 			    (concat
-			     (if (string-match "^[$@]"(car eqn)) "" "$")
+			     (if (or (string-prefix-p "$" (car eqn))
+				     (string-prefix-p "@" (car eqn)))
+				 ""
+			       "$")
 			     (car eqn) "=" (cdr eqn))))
 		 "")))))
 
@@ -1579,7 +1582,7 @@ With prefix ABOVE, insert above the current line."
   (if (not (org-at-table-p))
       (user-error "Not at a table"))
   (when (eobp) (insert "\n") (backward-char 1))
-  (if (not (string-match "|[ \t]*$" (org-current-line-string)))
+  (if (not (string-match-p "|[ \t]*$" (org-current-line-string)))
       (org-table-align))
   (let ((line (org-table-clean-line
 	       (buffer-substring (point-at-bol) (point-at-eol))))
@@ -2185,7 +2188,7 @@ with \"=\" or \":=\"."
 		      (assoc ref stored-list)
 		      (assoc scol stored-list))))
 	(cond (key (car ass))
-	      (ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
+	      (ass (concat (if (string-match-p "^[0-9]+$" (car ass)) "=" ":=")
 			   (cdr ass))))))
      (noerror nil)
      (t (error "No formula active for the current field")))))
@@ -2209,7 +2212,7 @@ When NAMED is non-nil, look for a named equation."
 	 (org-table-may-need-update nil)
 	 (stored (cdr (assoc scol stored-list)))
 	 (eq (cond
-	      ((and stored equation (string-match "^ *=? *$" equation))
+	      ((and stored equation (string-match-p "^ *=? *$" equation))
 	       stored)
 	      ((stringp equation)
 	       equation)

+ 1 - 1
lisp/org.el

@@ -11283,7 +11283,7 @@ of matched result, with is either `dedicated' or `fuzzy'."
   (cond
    ((not org-display-internal-link-with-indirect-buffer)
     buffer)
-   ((string-match "(Clone)$" (buffer-name buffer))
+   ((string-suffix-p "(Clone)" (buffer-name buffer))
     (message "Buffer is already a clone, not making another one")
     ;; we also do not modify visibility in this case
     buffer)

+ 2 - 2
lisp/ox-publish.el

@@ -412,9 +412,9 @@ This splices all the components into the list."
 	(pcase org-publish-sitemap-sort-files
 	  (`alphabetically
 	   (let* ((adir (file-directory-p a))
-		  (aorg (and (string-match "\\.org$" a) (not adir)))
+		  (aorg (and (string-suffix-p ".org" a) (not adir)))
 		  (bdir (file-directory-p b))
-		  (borg (and (string-match "\\.org$" b) (not bdir)))
+		  (borg (and (string-suffix-p ".org" b) (not bdir)))
 		  (A (if aorg (concat (file-name-directory a)
 				      (org-publish-find-title a)) a))
 		  (B (if borg (concat (file-name-directory b)

+ 1 - 1
lisp/ox-texinfo.el

@@ -585,7 +585,7 @@ holding export options."
 		 (let ((dirdesc
 			(let ((desc (plist-get info :texinfo-dirdesc)))
 			  (cond ((not desc) nil)
-				((string-match-p "\\.$" desc) desc)
+				((string-suffix-p "." desc) desc)
 				(t (concat desc "."))))))
 		   (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
 		 "\n"