Browse Source

Merge branch 'master' of orgmode.org:org-mode

Eric Schulte 7 years ago
parent
commit
8d9f7c4208
8 changed files with 269 additions and 108 deletions
  1. 12 12
      Makefile
  2. 54 9
      doc/org.texi
  3. 23 8
      lisp/org-capture.el
  4. 22 3
      lisp/org-clock.el
  5. 8 2
      lisp/org-html.el
  6. 2 0
      lisp/org-latex.el
  7. 8 6
      lisp/org-list.el
  8. 140 68
      lisp/org.el

+ 12 - 12
Makefile

@@ -238,30 +238,30 @@ lisp/org-install.el: $(LISPFILES0) Makefile
 	mv org-install.el lisp
 
 doc/org: doc/org.texi
-	(cd doc; $(MAKEINFO) --no-split org.texi -o org)
+	(cd doc && $(MAKEINFO) --no-split org.texi -o org)
 
 doc/org.pdf: doc/org.texi
-	(cd doc; $(TEXI2PDF) org.texi)
+	(cd doc && $(TEXI2PDF) org.texi)
 
 doc/orgguide.pdf: doc/orgguide.texi
-	(cd doc; $(TEXI2PDF) orgguide.texi)
+	(cd doc && $(TEXI2PDF) orgguide.texi)
 
 doc/org.html: doc/org.texi
-	(cd doc; $(TEXI2HTML) --no-split -o org.html org.texi)
+	(cd doc && $(TEXI2HTML) --no-split -o org.html org.texi)
 	UTILITIES/manfull.pl doc/org.html
 
 doc/orgcard.pdf: doc/orgcard.tex
-	(cd doc; pdftex orgcard.tex)
+	(cd doc && pdftex orgcard.tex)
 
 doc/orgcard.txt: doc/orgcard.tex
-	(cd doc; perl ../UTILITIES/orgcard2txt.pl orgcard.tex > orgcard.txt)
+	(cd doc && perl ../UTILITIES/orgcard2txt.pl orgcard.tex > orgcard.txt)
 
 doc/orgcard_letter.tex: doc/orgcard.tex
 	perl -pe 's/\\pdflayout=\(0l\)/\\pdflayout=(1l)/' \
                    doc/orgcard.tex > doc/orgcard_letter.tex
 
 doc/orgcard_letter.pdf: doc/orgcard_letter.tex
-	(cd doc; pdftex orgcard_letter.tex)
+	(cd doc && pdftex orgcard_letter.tex)
 
 # Below here are special targets for maintenance only
 
@@ -427,11 +427,11 @@ cleancontrib:
 cleanelc:
 	rm -f $(ELCFILES)
 cleandoc:
-	(cd doc; rm -f org.pdf org org.html orgcard.pdf orgguide.pdf)
-	(cd doc; rm -f *.aux *.cp *.cps *.dvi *.fn *.fns *.ky *.kys *.pg *.pgs)
-	(cd doc; rm -f *.toc *.tp *.tps *.vr *.vrs *.log *.html *.ps)
-	(cd doc; rm -f orgcard_letter.tex orgcard_letter.pdf)
-	(cd doc; rm -rf manual)
+	-(cd doc && rm -f org.pdf org org.html orgcard.pdf orgguide.pdf)
+	-(cd doc && rm -f *.aux *.cp *.cps *.dvi *.fn *.fns *.ky *.kys *.pg *.pgs)
+	-(cd doc && rm -f *.toc *.tp *.tps *.vr *.vrs *.log *.html *.ps)
+	-(cd doc && rm -f orgcard_letter.tex orgcard_letter.pdf)
+	-(cd doc && rm -rf manual)
 
 cleanrel:
 	rm -rf RELEASEDIR

+ 54 - 9
doc/org.texi

@@ -5917,6 +5917,9 @@ but you can specify your own function using the @code{:formatter} parameter.
              @r{All are overwritten except if there is an explicit @code{:narrow}}
 :timestamp   @r{A timestamp for the entry, when available.  Look for SCHEDULED,}
              @r{DEADLINE, TIMESTAMP and TIMESTAMP_IA, in this order.}
+:properties  @r{List of properties that should be shown in the table.  Each}
+             @r{property will get its own column.}
+:inherit-props @r{When this flag is @code{t}, the values for @code{:properties} will be inherited.}
 :formula     @r{Content of a @code{#+TBLFM} line to be added and evaluated.}
              @r{As a special case, @samp{:formula %} adds a column with % time.}
              @r{If you do not specify a formula here, any existing formula}
@@ -10160,13 +10163,14 @@ All lines between these markers are exported literally
 @subsection Tables in @LaTeX{} export
 @cindex tables, in @LaTeX{} export
 
-For @LaTeX{} export of a table, you can specify a label and a caption
-(@pxref{Images and tables}).  You can also use the @code{ATTR_LaTeX} line to
-request a @code{longtable} environment for the table, so that it may span
-several pages, or to change the default table environment from @code{table}
-to @code{table*} or to change the default inner tabular environment to
-@code{tabularx} or @code{tabulary}.  Finally, you can set the alignment
-string, and (with @code{tabularx} or @code{tabulary}) the width:
+For @LaTeX{} export of a table, you can specify a label, a caption and
+placement options (@pxref{Images and tables}).  You can also use the
+@code{ATTR_LaTeX} line to request a @code{longtable} environment for the
+table, so that it may span several pages, or to change the default table
+environment from @code{table} to @code{table*} or to change the default inner
+tabular environment to @code{tabularx} or @code{tabulary}.  Finally, you can
+set the alignment string, and (with @code{tabularx} or @code{tabulary}) the
+width:
 
 @cindex #+CAPTION
 @cindex #+LABEL
@@ -10207,7 +10211,20 @@ element.  You can use an @code{#+ATTR_LaTeX:} line to specify the various
 options that can be used in the optional argument of the
 @code{\includegraphics} macro.  To modify the placement option of the
 @code{figure} environment, add something like @samp{placement=[h!]} to the
-Attributes.
+Attributes. It is to be noted this option can be used with tables as well.
+The options are passed as the placement option to floating environments like
+@code{figure} or @code{table}. One can pass other compatible options as well.
+For example the @code{#+ATTR_LaTeX:} line below is exported as the
+@code{figure} environment below it.
+
+@cindex #+ATTR_LaTeX
+@example
+#+ATTR_LaTeX: placement=[<options>]\footnotesize
+
+\begin{figure}[<options>]\footnotesize
+...
+\end{figure}
+@end example
 
 If you would like to let text flow around the image, add the word @samp{wrap}
 to the @code{#+ATTR_LaTeX:} line, which will make the figure occupy the left
@@ -12911,6 +12928,7 @@ emacs -Q --batch -l $ORGINSTALL \
 * Clean view::                  Getting rid of leading stars in the outline
 * TTY keys::                    Using Org on a tty
 * Interaction::                 Other Emacs packages
+* org-crypt.el::                Encrypting Org files
 @end menu
 
 
@@ -13588,7 +13606,7 @@ tty you would rather use @kbd{C-c .} to re-insert the timestamp.
 @end multitable
 
 
-@node Interaction,  , TTY keys, Miscellaneous
+@node Interaction, org-crypt.el, TTY keys, Miscellaneous
 @section Interaction with other packages
 @cindex packages, interaction with other
 Org lives in the world of GNU Emacs and interacts in various ways
@@ -13797,6 +13815,33 @@ another key for this command, or override the key in
 
 @end table
 
++@node org-crypt.el,  , Interaction, Miscellaneous
++@section org-crypt.el
++@cindex @file{org-crypt.el}
++@cindex @code{org-decrypt-entry}
+
+Org-crypt will encrypt the text of an entry, but not the headline, or
+properties. Org-crypt uses the Emacs EasyPG library to encrypt and decrypt
+files.
+
+Any text below a headline that has a @samp{:crypt:} tag will be
+automatically be encrypted when the file is saved. If you want to use a
+different tag just customize the @code{org-crypt-tag-matcher} setting.
+
+To use org-crypt it is suggested that you have the following in your
+@file{.emacs}:
+
+@example
+(require 'org-crypt)
+(org-crypt-use-before-save-magic)
+(setq org-tags-exclude-from-inheritance (quote ("crypt")))
+;; GPG key to use for encryption
+;; Either the Key ID or set to nil to use symmetric encryption.
+(setq org-crypt-key nil)
+@end example
+
+Excluding the crypt tag from inheritance prevents already encrypted text
+being encrypted again.
 
 @node Hacking, MobileOrg, Miscellaneous, Top
 @appendix Hacking

+ 23 - 8
lisp/org-capture.el

@@ -343,23 +343,37 @@ The remember buffer is still current when this hook runs."
 
 (defvar org-capture-plist nil
   "Plist for the current capture process, global, to avoid having to pass it.")
+
 (defvar org-capture-current-plist nil
   "Local variable holding the plist in a capture buffer.
-This is used to store the plist for use when finishing a capture process.
-Another such process might have changed the global variable by then.")
+This is used to store the plist for use when finishing a capture process
+because another such process might have changed the global variable by then.
+
+Each time a new capture buffer has been set up, the global `org-capture-plist'
+is copied to this variable, which is local in the indirect buffer.")
+
 (defvar org-capture-clock-keep nil
   "Local variable to store the value of the :clock-keep parameter.
 This is needed in case org-capture-finalize is called interactively.")
 
 (defun org-capture-put (&rest stuff)
+  "Add properties to the capture property list `org-capture-plist'."
   (while stuff
     (setq org-capture-plist (plist-put org-capture-plist
 				       (pop stuff) (pop stuff)))))
 (defun org-capture-get (prop &optional local)
+  "Get properties from the capture property list `org-capture-plist'.
+When LOCAL is set, use the local variable `org-capture-current-plist',
+this is necessary after initialization of the capture process,
+to avoid conflicts with other active capture processes."
   (plist-get (if local org-capture-current-plist org-capture-plist) prop))
 
-(defun org-capture-member (prop)
-  (plist-get org-capture-plist prop))
+(defun org-capture-member (prop &optional local)
+  "Is PROP a preperty in `org-capture-plist'.
+When LOCAL is set, use the local variable `org-capture-current-plist',
+this is necessary after initialization of the capture process,
+to avoid conflicts with other active capture processes."
+  (plist-get (if local org-capture-current-plist org-capture-plist) prop))
 
 ;;; The minor mode
 
@@ -745,16 +759,17 @@ already gone.  Any prefix argument will be passed to the refile command."
 	(org-datetree-find-date-create
 	 (calendar-gregorian-from-absolute
 	  (cond
-
 	   (org-overriding-default-time
 	    ;; use the overriding default time
 	    (time-to-days org-overriding-default-time))
 
 	   ((eq (car target) 'file+datetree+prompt)
 	    ;; prompt for date
-	    (time-to-days (org-read-date
-			   nil t nil "Date for tree entry:"
-			   (current-time))))
+	    (let ((prompt-time (org-read-date
+				nil t nil "Date for tree entry:"
+				(current-time))))
+	      (org-capture-put :prompt-time prompt-time)
+	      (time-to-days prompt-time)))
 	   (t
 	    ;; current date, possible corrected for late night workers
 	    (org-today))))))

+ 22 - 3
lisp/org-clock.el

@@ -1,6 +1,6 @@
 ;;; org-clock.el --- The time clocking code for Org-mode
 
-;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
+;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 ;;   Free Software Foundation, Inc.
 
 ;; Author: Carsten Dominik <carsten at orgmode dot org>
@@ -2059,6 +2059,7 @@ from the dynamic block defintion."
 	 (emph (plist-get params :emphasize))
 	 (level-p (plist-get params :level))
 	 (timestamp (plist-get params :timestamp))
+	 (properties (plist-get params :properties))
 	 (ntcol (max 1 (or (plist-get params :tcolumns) 100)))
 	 (rm-file-column (plist-get params :one-file-with-archives))
 	 (indent (plist-get params :indent))
@@ -2122,6 +2123,7 @@ from the dynamic block defintion."
 	 (if multifile "|" "")          ; file column, maybe
 	 (if level-p   "|" "")          ; level column, maybe
 	 (if timestamp "|" "")          ; timestamp column, maybe
+	 (if properties (make-string (length properties) ?|) "")  ;properties columns, maybe
 	 (format "<%d>| |\n" narrow)))  ; headline and time columns
 
       ;; Insert the table header line
@@ -2130,6 +2132,7 @@ from the dynamic block defintion."
        (if multifile (concat (nth 1 lwords) "|") "")  ; file column, maybe
        (if level-p   (concat (nth 2 lwords) "|") "")  ; level column, maybe
        (if timestamp (concat (nth 3 lwords) "|") "")  ; timestamp column, maybe
+       (if properties (concat (mapconcat 'identity properties "|") "|") "") ;properties columns, maybe
        (concat (nth 4 lwords) "|" 
 	       (nth 5 lwords) "|\n"))                 ; headline and time columns
 
@@ -2141,6 +2144,7 @@ from the dynamic block defintion."
 				         ; file column, maybe
        (if level-p   "|"      "")        ; level column, maybe
        (if timestamp "|"      "")        ; timestamp column, maybe
+       (if properties (make-string (length properties) ?|) "")  ;properties columns, maybe
        (concat "*" (nth 7 lwords) "*| ") ; instead of a headline
        "*"
        (org-minutes-to-hh:mm-string (or total-time 0)) ; the time
@@ -2164,6 +2168,7 @@ from the dynamic block defintion."
 		       (file-name-nondirectory (car tbl))
 		       (if level-p   "| " "") ; level column, maybe
 		       (if timestamp "| " "") ; timestamp column, maybe
+		       (if properties (make-string (length properties) ?|) "")  ;properties columns, maybe
 		       (org-minutes-to-hh:mm-string (nth 1 tbl))))) ; the time
 
 	    ;; Get the list of node entries and iterate over it
@@ -2188,6 +2193,11 @@ from the dynamic block defintion."
 	       (if multifile "|" "")    ; free space for file name column?
 	       (if level-p (format "%d|" (car entry)) "")   ; level, maybe
 	       (if timestamp (concat (nth 2 entry) "|") "") ; timestamp, maybe
+	       (if properties
+		   (concat
+		    (mapconcat
+		     (lambda (p) (or (cdr (assoc p (nth 4 entry))) ""))
+		     properties "|") "|") "")  ;properties columns, maybe
 	       (if indent (org-clocktable-indent-string level) "") ; indentation
 	       hlc headline hlc "|"                                ; headline
 	       (make-string (min (1- ntcol) (or (- level 1))) ?|)
@@ -2342,6 +2352,8 @@ TIME:      The sum of all time spend in this tree, in minutes.  This time
 	 (block (plist-get params :block))
 	 (link (plist-get params :link))
 	 (tags (plist-get params :tags))
+	 (properties (plist-get params :properties))
+	 (inherit-property-p (plist-get params :inherit-props))
 	 (matcher (if tags (cdr (org-make-tags-matcher tags))))
 	 cc range-text st p time level hdl props tsp tbl)
 
@@ -2395,8 +2407,15 @@ TIME:      The sum of all time spend in this tree, in minutes.  This time
 			  (or (cdr (assoc "SCHEDULED" props))
 			      (cdr (assoc "DEADLINE" props))
 			      (cdr (assoc "TIMESTAMP" props))
-			      (cdr (assoc "TIMESTAMP_IA" props)))))
-	      (when (> time 0) (push (list level hdl tsp time) tbl))))))
+			      (cdr (assoc "TIMESTAMP_IA" props))))
+	    props (when properties
+		    (remove nil
+			    (mapcar
+			     (lambda (p)
+			       (when (org-entry-get (point) p inherit-property-p)
+				 (cons p (org-entry-get (point) p inherit-property-p))))
+			     properties))))
+	      (when (> time 0) (push (list level hdl tsp time props) tbl))))))
       (setq tbl (nreverse tbl))
       (list file org-clock-file-total-minutes tbl))))
 

+ 8 - 2
lisp/org-html.el

@@ -1,6 +1,6 @@
 ;;; org-html.el --- HTML export for Org-mode
 
-;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
+;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 ;;   Free Software Foundation, Inc.
 
 ;; Author: Carsten Dominik <carsten at orgmode dot org>
@@ -605,6 +605,11 @@ with a link to this URL."
 	  (const :tag "Keep internal css" nil)
 	  (string :tag "URL or local href")))
 
+(defcustom org-export-content-div "content"
+  "The name of the container DIV that holds all the page contents."
+  :group 'org-export-htmlize
+  :type 'string)
+
 ;;; Hooks
 
 (defvar org-export-html-after-blockquotes-hook nil
@@ -1278,7 +1283,7 @@ lang=\"%s\" xml:lang=\"%s\">
 %s
 </head>
 <body>
-<div id=\"content\">
+<div id=\"%s\">
 %s
 "
 		 (format
@@ -1295,6 +1300,7 @@ lang=\"%s\" xml:lang=\"%s\">
 		 date author description keywords
 		 style
 		 mathjax
+		 org-export-content-div
 		 (if (or link-up link-home)
 		     (concat
 		      (format org-export-html-home/up-format

+ 2 - 0
lisp/org-latex.el

@@ -2443,6 +2443,8 @@ The conversion is made depending of STRING-BEFORE and STRING-AFTER."
 		 (add-text-properties (1- (length footnote-rpl))
 				      (length footnote-rpl)
 				      '(org-protected t) footnote-rpl)
+		 (put-text-property 0 (length footnote-rpl)
+				    'original-indentation 1000 footnote-rpl)
 		 (if (org-on-heading-p)
 		     (setq footnote-rpl
 			   (concat (org-export-latex-protect-string "\\protect")

+ 8 - 6
lisp/org-list.el

@@ -115,6 +115,7 @@
 (declare-function org-on-heading-p "org" (&optional invisible-ok))
 (declare-function org-previous-line-empty-p "org" ())
 (declare-function org-remove-if "org" (predicate seq))
+(declare-function org-reduced-level "org" (L))
 (declare-function org-show-subtree "org" ())
 (declare-function org-time-string-to-seconds "org" (s))
 (declare-function org-timer-hms-to-secs "org-timer" (hms))
@@ -2989,7 +2990,7 @@ with overruling parameters for `org-list-to-generic'."
 LIST is as returned by `org-list-parse-list'.  PARAMS is a property list
 with overruling parameters for `org-list-to-generic'."
   (let* ((rule (cdr (assq 'heading org-blank-before-new-entry)))
-	 (level (or (org-current-level) 0))
+	 (level (org-reduced-level (or (org-current-level) 0)))
 	 (blankp (or (eq rule t)
 		     (and (eq rule 'auto)
 			  (save-excursion
@@ -3000,11 +3001,12 @@ with overruling parameters for `org-list-to-generic'."
 	   ;; Return the string for the heading, depending on depth D
 	   ;; of current sub-list.
 	   (lambda (d)
-	     (concat
-	      (make-string (+ level
-			      (if org-odd-levels-only (* 2 (1+ d)) (1+ d)))
-			   ?*)
-	      " ")))))
+	     (let ((oddeven-level (+ level d 1)))
+	       (concat (make-string (if org-odd-levels-only
+					(1- (* 2 oddeven-level))
+				      oddeven-level)
+				    ?*)
+		       " "))))))
     (org-list-to-generic
      list
      (org-combine-plists

+ 140 - 68
lisp/org.el

@@ -2488,14 +2488,20 @@ Must have a larger ASCII number than `org-highest-priority'."
 
 (defcustom org-default-priority ?B
   "The default priority of TODO items.
-This is the priority an item get if no explicit priority is given."
+This is the priority an item gets if no explicit priority is given.
+When starting to cycle on an empty priority the first step in the cycle
+depends on `org-priority-start-cycle-with-default'.  The resulting first
+step priority must not exceed the range from `org-highest-priority' to
+`org-lowest-priority' which means that `org-default-priority' has to be
+in this range exclusive or inclusive the range boundaries."
   :group 'org-priorities
   :type 'character)
 
 (defcustom org-priority-start-cycle-with-default t
   "Non-nil means start with default priority when starting to cycle.
 When this is nil, the first step in the cycle will be (depending on the
-command used) one higher or lower that the default priority."
+command used) one higher or lower than the default priority.
+See also `org-default-priority'."
   :group 'org-priorities
   :type 'boolean)
 
@@ -7122,7 +7128,10 @@ first headline."
 (defun org-reduced-level (l)
   "Compute the effective level of a heading.
 This takes into account the setting of `org-odd-levels-only'."
-  (if org-odd-levels-only (1+ (floor (/ l 2))) l))
+  (cond
+   ((zerop l) 0)
+   (org-odd-levels-only (1+ (floor (/ l 2))))
+   (t l)))
 
 (defun org-level-increment ()
   "Return the number of stars that will be added or removed at a
@@ -12303,12 +12312,18 @@ ACTION can be `set', `up', `down', or a character."
 	(if (and (not have) (eq last-command this-command))
 	    (setq new org-lowest-priority)
 	  (setq new (if (and org-priority-start-cycle-with-default (not have))
-			org-default-priority (1- current)))))
+			org-default-priority (1- current)))
+	  (when (< (upcase new) org-highest-priority)
+	    (error
+	     "See `org-default-priority' for range limit exceeded here"))))
        ((eq action 'down)
 	(if (and (not have) (eq last-command this-command))
 	    (setq new org-highest-priority)
 	  (setq new (if (and org-priority-start-cycle-with-default (not have))
-			org-default-priority (1+ current)))))
+			org-default-priority (1+ current)))
+	  (when (> (upcase new) org-lowest-priority)
+	    (error
+	     "See `org-default-priority' for range limit exceeded here"))))
        (t (error "Invalid action")))
       (if (or (< (upcase new) org-highest-priority)
 	      (> (upcase new) org-lowest-priority))
@@ -16919,7 +16934,7 @@ overwritten, and the table is not marked as requiring realignment."
 	  (if (>= org-self-insert-command-undo-counter 20)
 	      (setq org-self-insert-command-undo-counter 1)
 	    (and (> org-self-insert-command-undo-counter 0)
-		 buffer-undo-list
+		 buffer-undo-list (listp buffer-undo-list)
 		 (not (cadr buffer-undo-list)) ; remove nil entry
 		 (setcdr buffer-undo-list (cddr buffer-undo-list)))
 	    (setq org-self-insert-command-undo-counter
@@ -17762,65 +17777,113 @@ Calls `org-table-insert-hline', `org-toggle-item', or
 If there is no active region, only the current line is considered.
 
 If the first non blank line in the region is an headline, convert
-all headlines to items.
+all headlines to items, shifting text accordingly.
 
 If it is an item, convert all items to normal lines.
 
 If it is normal text, change region into an item. With a prefix
 argument ARG, change each line in region into an item."
   (interactive "P")
-  (let (l2 l beg end)
+  (let ((shift-text
+	 (function
+	  ;; Shift text in current section to IND, from point to END.
+	  ;; The function leaves point to END line.
+	  (lambda (ind end)
+	    (let ((min-i 1000) (end (copy-marker end)))
+	      ;; First determine the minimum indentation (MIN-I) of
+	      ;; the text.
+	      (save-excursion
+		(catch 'exit
+		  (while (< (point) end)
+		    (let ((i (org-get-indentation)))
+		      (cond
+		       ;; Skip blank lines and inline tasks.
+		       ((looking-at "^[ \t]*$"))
+		       ((looking-at "^\\*+ "))
+		       ;; We can't find less than 0 indentation.
+		       ((zerop i) (throw 'exit (setq min-i 0)))
+		       ((< i min-i) (setq min-i i))))
+		    (forward-line))))
+	      ;; Then indent each line so that a line indented to
+	      ;; MIN-I becomes indented to IND.  Ignore blank lines
+	      ;; and inline tasks in the process.
+	      (let ((delta (- ind min-i)))
+		(while (< (point) end)
+		  (unless (or (looking-at "^[ \t]*$")
+			      (looking-at "^\\*+ "))
+		    (org-indent-line-to (+ (org-get-indentation) delta)))
+		  (forward-line)))))))
+	(skip-blanks
+	 (function
+	  ;; Return beginning of first non-blank line, starting from
+	  ;; line at POS.
+	  (lambda (pos)
+	    (save-excursion
+	      (goto-char pos)
+	      (skip-chars-forward " \r\t\n")
+	      (point-at-bol)))))
+	beg end)
+    ;; Determine boundaries of changes.
     (if (org-region-active-p)
-	(setq beg (region-beginning) end (region-end))
-      (setq beg (point-at-bol)
-	    end (min (1+ (point-at-eol)) (point-max))))
+	(setq beg (funcall skip-blanks (region-beginning))
+	      end (copy-marker (region-end)))
+      (setq beg (funcall skip-blanks (point-at-bol))
+	    end (copy-marker (point-at-eol))))
+    ;; Depending on the starting line, choose an action on the text
+    ;; between BEG and END.
     (org-with-limited-levels
      (save-excursion
-       (goto-char end)
-       (setq l2 (org-current-line))
        (goto-char beg)
-       (beginning-of-line 1)
-       ;; Ignore blank lines at beginning of region
-       (skip-chars-forward " \t\r\n")
-       (beginning-of-line 1)
-       (setq l (1- (org-current-line)))
        (cond
-	;; Case 1. Start at an item: de-itemize.
+	;; Case 1. Start at an item: de-itemize. Note that it only
+	;;         happens when a region is active: `org-ctrl-c-minus'
+	;;         would call `org-cycle-list-bullet' otherwise.
 	((org-at-item-p)
-	 (while (< (setq l (1+ l)) l2)
+	 (while (< (point) end)
 	   (when (org-at-item-p)
 	     (skip-chars-forward " \t")
 	     (delete-region (point) (match-end 0)))
-	   (beginning-of-line 2)))
-	;; Case 2. Start an an heading: convert to items.
+	   (forward-line)))
+	;; Case 2. Start at an heading: convert to items.
 	((org-on-heading-p)
 	 (let* ((bul (org-list-bullet-string "-"))
-		(len (length bul))
-		(ind 0) (level 0))
-	   (while (< (setq l (1+ l)) l2)
-	     (cond
-	      ((looking-at outline-regexp)
-	       (let* ((lvl (org-reduced-level
-			    (- (length (match-string 0)) 2)))
-		      (s (concat (make-string (* len lvl) ? ) bul)))
-		 (replace-match s t t)
-		 (setq ind (length s) level lvl)))
-	      ;; Ignore blank lines and inline tasks.
-	      ((looking-at "^[ \t]*$"))
-	      ((looking-at "^\\*+ "))
-	      ;; Ensure normal text belongs to the new item.
-	      (t (org-indent-line-to (+ (max (- (org-get-indentation) level 2) 0)
-					ind))))
-	     (beginning-of-line 2))))
-	;; Case 3. Normal line with ARG: turn each of them into items
-	;;         unless they are already one.
+		(bul-len (length bul))
+		;; Indentation of the first heading.  It should be
+		;; relative to the indentation of its parent, if any.
+		(start-ind (save-excursion
+			     (cond
+			      ((not org-adapt-indentation) 0)
+			      ((not (outline-previous-heading)) 0)
+			      (t (length (match-string 0))))))
+		;; Level of first heading. Further headings will be
+		;; compared to it to determine hierarchy in the list.
+		(ref-level (org-reduced-level (org-outline-level))))
+	   (while (< (point) end)
+	     (let* ((level (org-reduced-level (org-outline-level)))
+		    (delta (max 0 (- level ref-level))))
+	       ;; If current headline is less indented than the first
+	       ;; one, set it as reference, in order to preserve
+	       ;; subtrees.
+	       (when (< level ref-level) (setq ref-level level))
+	       (replace-match bul t t)
+	       (org-indent-line-to (+ start-ind (* delta bul-len)))
+	       ;; Ensure all text down to END (or SECTION-END) belongs
+	       ;; to the newly created item.
+	       (let ((section-end (save-excursion
+				    (or (outline-next-heading) (point)))))
+		 (forward-line)
+		 (funcall shift-text
+			  (+ start-ind (* (1+ delta) bul-len))
+			  (min end section-end)))))))
+	;; Case 3. Normal line with ARG: turn each non-item line into
+	;;         an item.
 	(arg
-	 (while (< (setq l (1+ l)) l2)
+	 (while (< (point) end)
 	   (unless (or (org-on-heading-p) (org-at-item-p))
 	     (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
 		 (replace-match
 		  (concat "\\1" (org-list-bullet-string "-") "\\2"))))
-	   (beginning-of-line 2)))
+	   (forward-line)))
 	;; Case 4. Normal line without ARG: make the first line of
 	;;         region an item, and shift indentation of others
 	;;         lines to set them as item's body.
@@ -17829,13 +17892,15 @@ argument ARG, change each line in region into an item."
 		  (ref-ind (org-get-indentation)))
 	     (skip-chars-forward " \t")
 	     (insert bul)
-	     (beginning-of-line 2)
-	     (while (and (< (setq l (1+ l)) l2) (< (point) end))
+	     (forward-line)
+	     (while (< (point) end)
 	       ;; Ensure that lines less indented than first one
 	       ;; still get included in item body.
-	       (org-indent-line-to (+ (max ref-ind (org-get-indentation))
-				      bul-len))
-	       (beginning-of-line 2)))))))))
+	       (funcall shift-text
+			(+ ref-ind bul-len)
+			(min end (save-excursion (or (outline-next-heading)
+						     (point)))))
+	       (forward-line)))))))))
 
 (defun org-toggle-heading (&optional nstars)
   "Convert headings to normal text, or items or text to headings.
@@ -17854,29 +17919,36 @@ such that the lines become children of the current entry.  However,
 when a prefix argument is given, its value determines the number of
 stars to add."
   (interactive "P")
-  (let (l2 l itemp beg end)
+  (let ((skip-blanks
+	 (function
+	  ;; Return beginning of first non-blank line, starting from
+	  ;; line at POS.
+	  (lambda (pos)
+	    (save-excursion
+	      (goto-char pos)
+	      (skip-chars-forward " \r\t\n")
+	      (point-at-bol)))))
+	beg end)
+    ;; Determine boundaries of changes. If region ends at a bol, do
+    ;; not consider the last line to be in the region.
     (if (org-region-active-p)
-	(setq beg (region-beginning) end (copy-marker (region-end)))
-      (setq beg (point-at-bol)
-	    end (min (1+ (point-at-eol)) (point-max))))
+	(setq beg (funcall skip-blanks (region-beginning))
+	      end (copy-marker (save-excursion
+				 (goto-char (region-end))
+				 (if (bolp) (point) (point-at-eol)))))
+      (setq beg (funcall skip-blanks (point-at-bol))
+	    end (copy-marker (point-at-eol))))
     ;; Ensure inline tasks don't count as headings.
     (org-with-limited-levels
      (save-excursion
-       (goto-char end)
-       (setq l2 (org-current-line))
        (goto-char beg)
-       (beginning-of-line 1)
-       ;; Ignore blank lines at beginning of region
-       (skip-chars-forward " \t\r\n")
-       (beginning-of-line 1)
-       (setq l (1- (org-current-line)))
        (cond
 	;; Case 1. Started at an heading: de-star headings.
 	((org-on-heading-p)
-	 (while (< (setq l (1+ l)) l2)
+	 (while (< (point) end)
 	   (when (org-on-heading-p t)
 	     (looking-at outline-regexp) (replace-match ""))
-	   (beginning-of-line 2)))
+	   (forward-line)))
 	;; Case 2. Started at an item: change items into headlines.
 	((org-at-item-p)
 	 (let ((stars (make-string
@@ -17888,7 +17960,7 @@ stars to add."
 	     (when (org-at-item-p)
 	       ;; Pay attention to cases when region ends before list.
 	       (let* ((struct (org-list-struct))
-		      (list-end (min (org-list-get-bottom-point struct) end)))
+		      (list-end (min (org-list-get-bottom-point struct) (1+ end))))
 		 (save-restriction
 		   (narrow-to-region (point) list-end)
 		   (insert
@@ -17897,7 +17969,7 @@ stars to add."
 		     '(:istart (concat stars (funcall get-stars depth))
 			       :icount (concat stars
 					       (funcall get-stars depth))))))))
-	     (beginning-of-line 2))))
+	     (forward-line))))
 	;; Case 3. Started at normal text: make every line an heading,
 	;;         skipping headlines and items.
 	(t (let* ((stars (make-string
@@ -17910,11 +17982,11 @@ stars to add."
 				   (org-odd-levels-only "**")
 				   (t "*")))
 		  (rpl (concat stars add-stars " ")))
-	     (while (< (setq l (1+ l)) l2)
-	       (unless (or (org-on-heading-p) (org-at-item-p))
-		 (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
-		   (replace-match (concat rpl (match-string 2)))))
-	       (beginning-of-line 2)))))))))
+	     (while (< (point) end)
+	       (when (and (not (org-on-heading-p)) (not (org-at-item-p))
+			  (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
+		 (replace-match (concat rpl (match-string 2))))
+	       (forward-line)))))))))
 
 (defun org-meta-return (&optional arg)
   "Insert a new heading or wrap a region in a table.