浏览代码

ol: Refactor code for clarity

* lisp/ol.el (org-link--normalize-string): New function.
(org-link--squeeze-white-spaces, org-link--clear-syntax-from-context):
Remove functions (merged in the previous one).
(org-link-search):
(org-link-heading-search-string):
(org-store-link): Use new function.
Nicolas Goaziou 5 年之前
父节点
当前提交
6ea1ca51a8
共有 1 个文件被更改,包括 36 次插入36 次删除
  1. 36 36
      lisp/ol.el

+ 36 - 36
lisp/ol.el

@@ -737,21 +737,28 @@ White spaces are not significant."
 		(mapconcat #'identity
 		(mapconcat #'identity
 			   (cl-subseq lines 0 org-link-context-for-files)
 			   (cl-subseq lines 0 org-link-context-for-files)
 			   "\n"))))
 			   "\n"))))
-      (org-link--squeeze-white-spaces context))))
-
-(defun org-link--squeeze-white-spaces (string)
-  "Trim STRING, pack contiguous white spaces, and return it."
-  (replace-regexp-in-string "[ \t\n]+" " " (org-trim string)))
-
-(defun org-link--clear-syntax-from-context (context)
-  "Remove special syntax from CONTEXT string and return it."
-  (while (cond ((and (string-prefix-p "(" context)
-		     (string-suffix-p ")" context))
-		(setq context (substring context 1 -1)))
-	       ((string-match "\\`[#*]+" context)
-		(setq context (substring context (match-end 0))))
-	       (t nil)))
-  context)
+      context)))
+
+(defun org-link--normalize-string (string &optional context)
+  "Remove ignored contents from STRING string and return it.
+This function removes contiguous white spaces and statistics
+cookies.  When optional argument CONTEXT is non-nil, it assumes
+STRING is a context string, and also removes special search
+syntax around the string."
+  (when context
+    (while (cond ((and (string-prefix-p "(" string)
+		       (string-suffix-p ")" string))
+		  (setq string (substring string 1 -1)))
+		 ((string-match "\\`[#*]+" string)
+		  (setq string (substring string (match-end 0))))
+		 (t nil))))
+  (org-trim
+   (replace-regexp-in-string
+    (rx (or (one-or-more (any " \t"))
+	    ;; Statistics cookie regexp.
+	    (seq "[" (0+ digit) (or "%" (seq "/" (0+ digit))) "]")))
+    " "
+    string)))
 
 
 
 
 ;;; Public API
 ;;; Public API
@@ -1179,15 +1186,14 @@ of matched result, which is either `dedicated' or `fuzzy'."
 		  (format "%s.*\\(?:%s[ \t]\\)?.*%s"
 		  (format "%s.*\\(?:%s[ \t]\\)?.*%s"
 			  org-outline-regexp-bol
 			  org-outline-regexp-bol
 			  org-comment-string
 			  org-comment-string
-			  (mapconcat #'regexp-quote words ".+")))
-		 (cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"))
+			  (mapconcat #'regexp-quote words ".+"))))
 	     (goto-char (point-min))
 	     (goto-char (point-min))
 	     (catch :found
 	     (catch :found
 	       (while (re-search-forward title-re nil t)
 	       (while (re-search-forward title-re nil t)
 		 (when (equal words
 		 (when (equal words
 			      (split-string
 			      (split-string
-			       (replace-regexp-in-string
-				cookie-re " " (org-get-heading t t t t))))
+			       (org-link--normalize-string
+				(org-get-heading t t t t))))
 		   (throw :found t)))
 		   (throw :found t)))
 	       nil)))
 	       nil)))
       (beginning-of-line)
       (beginning-of-line)
@@ -1247,15 +1253,12 @@ into a single one.
 When optional argument STRING is non-nil, assume it a headline,
 When optional argument STRING is non-nil, assume it a headline,
 without any TODO or COMMENT keyword, and without any priority
 without any TODO or COMMENT keyword, and without any priority
 cookie or tag."
 cookie or tag."
-  (let ((cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]")
-	(context
-	 (if (not string)
-	     (concat "*" (org-trim (org-get-heading t t t t)))
-	   (let ((s (org-trim string)))
-	     (if (string-prefix-p "*" s) s
-	       (setq s (concat "*" s)))))))
-    (org-link--squeeze-white-spaces
-     (replace-regexp-in-string cookie-re " " context))))
+  (org-link--normalize-string
+   (if (not string)
+       (concat "*" (org-trim (org-get-heading t t t t)))
+     (let ((s (org-trim string)))
+       (if (string-prefix-p "*" s) s
+	 (concat "*" s))))))
 
 
 (defun org-link-open-as-file (path arg)
 (defun org-link-open-as-file (path arg)
   "Pretend PATH is a file name and open it.
   "Pretend PATH is a file name and open it.
@@ -1633,13 +1636,10 @@ non-nil."
 		    (context
 		    (context
 		     (cond
 		     (cond
 		      ((let ((region (org-link--context-from-region)))
 		      ((let ((region (org-link--context-from-region)))
-			 (and region
-			      (org-link--clear-syntax-from-context region))))
+			 (and region (org-link--normalize-string region t))))
 		      (name)
 		      (name)
 		      ((org-before-first-heading-p)
 		      ((org-before-first-heading-p)
-		       (org-link--clear-syntax-from-context
-			(org-link--squeeze-white-spaces
-			 (org-current-line-string))))
+		       (org-link--normalize-string (org-current-line-string) t))
 		      (t (org-link-heading-search-string)))))
 		      (t (org-link-heading-search-string)))))
 	       (when (org-string-nw-p context)
 	       (when (org-string-nw-p context)
 		 (setq cpltxt (format "%s::%s" cpltxt context))
 		 (setq cpltxt (format "%s::%s" cpltxt context))
@@ -1653,10 +1653,10 @@ non-nil."
 			      (buffer-file-name (buffer-base-buffer)))))
 			      (buffer-file-name (buffer-base-buffer)))))
 	;; Add a context search string.
 	;; Add a context search string.
 	(when (org-xor org-link-context-for-files (equal arg '(4)))
 	(when (org-xor org-link-context-for-files (equal arg '(4)))
-	  (let ((context (org-link--clear-syntax-from-context
+	  (let ((context (org-link--normalize-string
 			  (or (org-link--context-from-region)
 			  (or (org-link--context-from-region)
-			      (org-link--squeeze-white-spaces
-			       (org-current-line-string))))))
+			      (org-current-line-string))
+			  t)))
 	    ;; Only use search option if there is some text.
 	    ;; Only use search option if there is some text.
 	    (when (org-string-nw-p context)
 	    (when (org-string-nw-p context)
 	      (setq cpltxt (format "%s::%s" cpltxt context))
 	      (setq cpltxt (format "%s::%s" cpltxt context))