Browse Source

Refactoring

Nicolas Goaziou 14 years ago
parent
commit
bed92c6863
1 changed files with 24 additions and 49 deletions
  1. 24 49
      lisp/org-list.el

+ 24 - 49
lisp/org-list.el

@@ -476,9 +476,13 @@ function ends."
 	     (not list-ender))))))
 	     (not list-ender))))))
 
 
 (defun org-list-first-item-p ()
 (defun org-list-first-item-p ()
-  "Is this item the first item in a plain list?"
+  "Is this item the first item in a plain list?
+Assume point is at an item."
   (save-excursion
   (save-excursion
-    (= (org-beginning-of-item) (org-beginning-of-item-list))))
+    (beginning-of-line)
+    (let ((ind (org-get-indentation)))
+      (or (not (org-search-backward-unenclosed org-item-beginning-re (org-list-top-point) t))
+	  (< (org-get-indentation) ind)))))
 
 
 (defun org-at-item-p ()
 (defun org-at-item-p ()
   "Is point in a line starting a hand-formatted item?"
   "Is point in a line starting a hand-formatted item?"
@@ -544,7 +548,7 @@ A checkbox is blocked if all of the following conditions are fulfilled:
   "Return point just before list ending or nil if not in a list."
   "Return point just before list ending or nil if not in a list."
   (save-excursion
   (save-excursion
     (and (org-in-item-p)
     (and (org-in-item-p)
-	 (let ((pos (org-beginning-of-item))
+	 (let ((pos (point))
 	       (bound (or (and (let ((outline-regexp org-outline-regexp))
 	       (bound (or (and (let ((outline-regexp org-outline-regexp))
 				 ;; Use default regexp because folding
 				 ;; Use default regexp because folding
 				 ;; changes OUTLINE-REGEXP.
 				 ;; changes OUTLINE-REGEXP.
@@ -570,12 +574,10 @@ If the cursor is not in an item, throw an error. Return point."
 
 
 (defun org-end-of-item ()
 (defun org-end-of-item ()
   "Go to the end of the current hand-formatted item.
   "Go to the end of the current hand-formatted item.
-If the cursor is not in an item, throw an error."
+If the cursor is not in an item, throw an error. Return point."
   (interactive)
   (interactive)
   (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
   (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
-    (cond ((not (org-in-item-p)) (error "Not in an item"))
-	  (next-p (goto-char next-p))
-	  (t (org-end-of-item-list)))))
+    (if next-p (goto-char next-p) (org-end-of-item-list))))
 
 
 (defun org-end-of-item-or-at-child ()
 (defun org-end-of-item-or-at-child ()
   "Move to the end of the item text, stops before the first child if any."
   "Move to the end of the item text, stops before the first child if any."
@@ -614,9 +616,7 @@ Item is at the same level in the current plain list. Error if not
 in a plain list, or if this is the last item in the list."
 in a plain list, or if this is the last item in the list."
   (interactive)
   (interactive)
   (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
   (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
-    (if next-p
-	(goto-char next-p)
-      (error "On last item"))))
+    (if next-p (goto-char next-p) (error "On last item"))))
 
 
 (defun org-previous-item ()
 (defun org-previous-item ()
   "Move to the beginning of the previous item.
   "Move to the beginning of the previous item.
@@ -624,24 +624,16 @@ Item is at the same level in the current plain list. Error if not
 in a plain list, or if this is the first item in the list."
 in a plain list, or if this is the first item in the list."
   (interactive)
   (interactive)
   (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
   (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
-    (if prev-p
-	(goto-char prev-p)
-      (error "On first item"))))
+    (if prev-p (goto-char prev-p) (error "On first item"))))
 
 
 (defun org-beginning-of-item-list ()
 (defun org-beginning-of-item-list ()
   "Go to the beginning item of the current list or sublist.
   "Go to the beginning item of the current list or sublist.
 Return point."
 Return point."
   (interactive)
   (interactive)
-  (org-beginning-of-item)
   (let ((limit (org-list-top-point))
   (let ((limit (org-list-top-point))
-	(move-up (lambda (pos bound)
-		   ;; prev-p: any item of same level before ?
-		   (let ((prev-p (org-get-previous-item pos bound)))
-		     ;; recurse until no more item of the same level
-		     ;; can be found.
-		     (if (not prev-p) pos (funcall move-up prev-p bound))))))
-    ;; Go to the last item found and at bol in case we didn't move
-    (goto-char (funcall move-up (point) limit))
+	prev-p)
+    (while (setq prev-p (org-get-previous-item (point) limit))
+      (goto-char prev-p))
     (goto-char (point-at-bol))))
     (goto-char (point-at-bol))))
 
 
 (defun org-end-of-item-list ()
 (defun org-end-of-item-list ()
@@ -650,22 +642,11 @@ Return point."
   (interactive)
   (interactive)
   (org-beginning-of-item)
   (org-beginning-of-item)
   (let ((limit (org-list-bottom-point))
   (let ((limit (org-list-bottom-point))
-	(ind (org-get-indentation))
-	(get-last-item (lambda (pos bound)
-			 ;; next-p: any item of same level after ?
-			 (let ((next-p (org-get-next-item pos bound)))
-			   ;; recurse until no more item of the same level
-			   ;; can be found.
-			   (if (not next-p) pos (funcall get-last-item next-p bound))))))
-    ;; Move to the last item of every list or sublist encountered, and
-    ;; down to bol of a higher-level item, or limit.
+	(ind (org-get-indentation)))
     (while (and (/= (point) limit)
     (while (and (/= (point) limit)
 		(>= (org-get-indentation) ind))
 		(>= (org-get-indentation) ind))
-      (goto-char (funcall get-last-item (point) limit))
-      (end-of-line)
-      (when (org-search-forward-unenclosed org-item-beginning-re limit 'move)
-	(beginning-of-line)))
-    (point)))
+      (org-search-forward-unenclosed org-item-beginning-re limit 'move))
+    (if (= (point) limit) limit (goto-char (point-at-bol)))))
 
 
 ;;; Manipulate
 ;;; Manipulate
 
 
@@ -694,7 +675,7 @@ so this really moves item trees."
   (let ((pos (point))
   (let ((pos (point))
 	(col (current-column))
 	(col (current-column))
 	(actual-item (org-beginning-of-item))
 	(actual-item (org-beginning-of-item))
-	(next-item (org-get-next-item (point) (save-excursion (org-end-of-item-list)))))
+	(next-item (org-get-next-item (point) (org-list-bottom-point))))
     (if (not next-item)
     (if (not next-item)
 	(progn
 	(progn
 	  (goto-char pos)
 	  (goto-char pos)
@@ -712,7 +693,7 @@ so this really moves item trees."
   (let ((pos (point))
   (let ((pos (point))
 	(col (current-column))
 	(col (current-column))
 	(actual-item (org-beginning-of-item))
 	(actual-item (org-beginning-of-item))
-	(prev-item (org-get-previous-item (point) (save-excursion (org-beginning-of-item-list)))))
+	(prev-item (org-get-previous-item (point) (org-list-top-point))))
     (if (not prev-item)
     (if (not prev-item)
 	(progn
 	(progn
 	  (goto-char pos)
 	  (goto-char pos)
@@ -917,16 +898,10 @@ This function modifies STRUCT."
 (defun org-list-struct-fix-struct (struct origins)
 (defun org-list-struct-fix-struct (struct origins)
   "Return STRUCT with correct bullets and indentation.
   "Return STRUCT with correct bullets and indentation.
 Only elements of STRUCT that have changed are returned."
 Only elements of STRUCT that have changed are returned."
-  (let ((before (copy-alist struct))
-	(set-diff (lambda (setA setB result)
-		    (cond
-		     ((null setA) result)
-		     ((equal (car setA) (car setB))
-		      (funcall set-diff (cdr setA) (cdr setB) result))
-		     (t (funcall set-diff (cdr setA) (cdr setB) (cons (car setA) result)))))))
+  (let ((old (copy-alist struct)))
     (org-list-struct-fix-bul struct origins)
     (org-list-struct-fix-bul struct origins)
     (org-list-struct-fix-ind struct origins)
     (org-list-struct-fix-ind struct origins)
-    (nreverse (funcall set-diff struct before nil))))
+    (delq nil (mapcar (lambda (e) (when (not (equal (pop old) e)) e)) struct))))
 
 
 (defun org-list-struct-outdent (start end origins)
 (defun org-list-struct-outdent (start end origins)
   "Outdent items in ORIGINS between BEGIN and END.
   "Outdent items in ORIGINS between BEGIN and END.
@@ -1168,13 +1143,13 @@ children. Return t if successful."
 	   (t (back-to-indentation)
 	   (t (back-to-indentation)
 	      (indent-to-column (car org-tab-ind-state))
 	      (indent-to-column (car org-tab-ind-state))
 	      (end-of-line)
 	      (end-of-line)
-	      (org-fix-bullet-type (nth 1 org-tab-ind-state))
+	      (org-fix-bullet-type (cdr org-tab-ind-state))
 	      ;; Break cycle
 	      ;; Break cycle
 	      (setq this-command 'identity)))
 	      (setq this-command 'identity)))
 	;; If a cycle is starting, remember indentation and bullet,
 	;; If a cycle is starting, remember indentation and bullet,
         ;; then try to indent. If it fails, try to outdent.
         ;; then try to indent. If it fails, try to outdent.
 	(setq org-tab-ind-state
 	(setq org-tab-ind-state
-              (list (org-get-indentation) (org-get-bullet)))
+              (cons (org-get-indentation) (org-get-bullet)))
 	(cond
 	(cond
 	 ((ignore-errors (org-indent-item 1)))
 	 ((ignore-errors (org-indent-item 1)))
 	 ((ignore-errors (org-indent-item -1)))
 	 ((ignore-errors (org-indent-item -1)))
@@ -1536,7 +1511,7 @@ will return the number of items in the current list.
 Sublists of the list are skipped. Cursor is always at the
 Sublists of the list are skipped. Cursor is always at the
 beginning of the item."
 beginning of the item."
   (save-excursion
   (save-excursion
-    (let ((end (copy-marker (save-excursion (org-end-of-item-list))))
+    (let ((end (copy-marker (org-list-bottom-point)))
 	  (next-p (make-marker))
 	  (next-p (make-marker))
 	  (move-down-action
 	  (move-down-action
 	   (lambda (pos value &rest args)
 	   (lambda (pos value &rest args)