Browse Source

Allow a new value for `org-adapt-indentation'

* doc/org-manual.org (Hard indentation): Add a footnote
mentioning the new value for `org-adapt-indentation'

* etc/ORG-NEWS: Mention the new value for
`org-adapt-indentation'.

* lisp/org.el (org-logbook-drawer-re): New constant.
(org-adapt-indentation): Allow 'headline-data as a new value
to only adapt indentation for headline data.
(org-fixup-indentation, org--get-expected-indentation): Handle
`org-adapt-indentation' set to 'headline-data.

* lisp/org-indent.el (org-indent-mode)
(org-indent-add-properties): Handle `org-adapt-indentation'
set to 'headline-data.

* lisp/org-clock.el (org-clock-in): Call `org-indent-line'
after clock timestamp insertion.
Bastien 5 years ago
parent
commit
e3b79ad2bf
5 changed files with 95 additions and 39 deletions
  1. 29 25
      doc/org-manual.org
  2. 9 0
      etc/ORG-NEWS
  3. 2 2
      lisp/org-clock.el
  4. 17 3
      lisp/org-indent.el
  5. 38 9
      lisp/org.el

+ 29 - 25
doc/org-manual.org

@@ -18580,7 +18580,8 @@ use =STARTUP= keyword as follows:
 It is possible to use hard spaces to achieve the indentation instead,
 if the bare ASCII file should have the indented look also outside
 Emacs[fn:147].  With Org's support, you have to indent all lines to
-line up with the outline headers.  You would use these settings:
+line up with the outline headers.  You would use these
+settings[fn:148]:
 
   #+begin_src emacs-lisp
   (setq org-adapt-indentation t
@@ -18843,7 +18844,7 @@ changes.
 
   #+vindex: org-startup-indented
   Dynamic virtual indentation is controlled by the variable
-  ~org-startup-indented~[fn:148].
+  ~org-startup-indented~[fn:149].
 
   | =indent=   | Start with Org Indent mode turned on.  |
   | =noindent= | Start with Org Indent mode turned off. |
@@ -19652,7 +19653,7 @@ Tags]]) only for those set in these variables.
 
 #+vindex: org-mobile-directory
 The mobile application needs access to a file directory on
-a server[fn:149] to interact with Emacs.  Pass its location through
+a server[fn:150] to interact with Emacs.  Pass its location through
 the ~org-mobile-directory~ variable.  If you can mount that directory
 locally just set the variable to point to that directory:
 
@@ -19673,7 +19674,7 @@ With a public server, consider encrypting the files.  Org also
 requires OpenSSL installed on the local computer.  To turn on
 encryption, set the same password in the mobile application and in
 Emacs.  Set the password in the variable
-~org-mobile-use-encryption~[fn:150].  Note that even after the mobile
+~org-mobile-use-encryption~[fn:151].  Note that even after the mobile
 application encrypts the file contents, the file name remains visible
 on the file systems of the local computer, the server, and the mobile
 device.
@@ -19689,15 +19690,15 @@ The command ~org-mobile-push~ copies files listed in
 ~org-mobile-files~ into the staging area.  Files include agenda files
 (as listed in ~org-agenda-files~).  Customize ~org-mobile-files~ to
 add other files.  File names are staged with paths relative to
-~org-directory~, so all files should be inside this directory[fn:151].
+~org-directory~, so all files should be inside this directory[fn:152].
 
 Push creates a special Org file =agendas.org= with custom agenda views
-defined by the user[fn:152].
+defined by the user[fn:153].
 
 Finally, Org writes the file =index.org=, containing links to other
 files.  The mobile application reads this file first from the server
 to determine what other files to download for agendas.  For faster
-downloads, it is expected to only read files whose checksums[fn:153]
+downloads, it is expected to only read files whose checksums[fn:154]
 have changed.
 
 *** Pulling from the mobile application
@@ -19714,7 +19715,7 @@ data in an inbox file format, through the following steps:
 
 1.
    #+vindex: org-mobile-inbox-for-pull
-   Org moves all entries found in =mobileorg.org=[fn:154] and appends
+   Org moves all entries found in =mobileorg.org=[fn:155] and appends
    them to the file pointed to by the variable
    ~org-mobile-inbox-for-pull~.  It should reside neither in the
    staging area nor on the server.  Each captured entry and each
@@ -20005,9 +20006,9 @@ of these strategies:
 #+cindex: @LaTeX{}, and Orgtbl mode
 
 To wrap a source table in LaTeX, use the =comment= environment
-provided by =comment.sty=[fn:155].  To activate it, put
+provided by =comment.sty=[fn:156].  To activate it, put
 ~\usepackage{comment}~ in the document header.  Orgtbl mode inserts
-a radio table skeleton[fn:156] with the command {{{kbd(M-x
+a radio table skeleton[fn:157] with the command {{{kbd(M-x
 orgtbl-insert-radio-table)}}}, which prompts for a table name.  For
 example, if =salesfigures= is the name, the template inserts:
 
@@ -20026,7 +20027,7 @@ The line =#+ORGTBL: SEND= tells Orgtbl mode to use the function
 ~orgtbl-to-latex~ to convert the table to LaTeX format, then insert
 the table at the target (receive) location named =salesfigures=.  Now
 the table is ready for data entry.  It can even use spreadsheet
-features[fn:157]:
+features[fn:158]:
 
 #+begin_example
 % BEGIN RECEIVE ORGTBL salesfigures
@@ -20242,7 +20243,7 @@ Dynamic blocks, like any other block, can be narrowed with
 #+vindex: org-agenda-skip-function
 #+vindex: org-agenda-skip-function-global
 Org provides a special hook to further limit items in agenda views:
-~agenda~, ~agenda*~[fn:158], ~todo~, ~alltodo~, ~tags~, ~tags-todo~,
+~agenda~, ~agenda*~[fn:159], ~todo~, ~alltodo~, ~tags~, ~tags-todo~,
 ~tags-tree~.  Specify a custom function that tests inclusion of every
 matched item in the view.  This function can also skip as much as is
 needed.
@@ -20285,7 +20286,7 @@ meaningful string suitable for the agenda view.
 #+vindex: org-agenda-skip-function
 Search for entries with a limit set on levels for the custom search.
 This is a general approach to creating custom searches in Org.  To
-include all levels, use =LEVEL>0=[fn:159].  Then to selectively pick
+include all levels, use =LEVEL>0=[fn:160].  Then to selectively pick
 the matched entries, use ~org-agenda-skip-function~, which also
 accepts Lisp forms, such as ~org-agenda-skip-entry-if~ and
 ~org-agenda-skip-subtree-if~.  For example:
@@ -21719,39 +21720,42 @@ through ~word-wrap~.
 [fn:147] This works, but requires extra effort.  Org Indent mode is
 more convenient for most applications.
 
-[fn:148] Note that Org Indent mode also sets the ~wrap-prefix~
+[fn:148] ~org-adapt-indentation~ can also be set to ='headline-data=,
+in which case only data lines below the headline will be indented.
+
+[fn:149] Note that Org Indent mode also sets the ~wrap-prefix~
 property, such that Visual Line mode (or purely setting ~word-wrap~)
 wraps long lines, including headlines, correctly indented.
 
-[fn:149] For a server to host files, consider using a WebDAV server,
+[fn:150] For a server to host files, consider using a WebDAV server,
 such as [[https://nextcloud.com][Nextcloud]].  Additional help is at this [[https://orgmode.org/worg/org-faq.html#mobileorg_webdav][FAQ entry]].
 
-[fn:150] If Emacs is configured for safe storing of passwords, then
+[fn:151] If Emacs is configured for safe storing of passwords, then
 configure the variable ~org-mobile-encryption-password~; please read
 the docstring of that variable.
 
-[fn:151] Symbolic links in ~org-directory~ need to have the same name
+[fn:152] Symbolic links in ~org-directory~ need to have the same name
 as their targets.
 
-[fn:152] While creating the agendas, Org mode forces =ID= properties
+[fn:153] While creating the agendas, Org mode forces =ID= properties
 on all referenced entries, so that these entries can be uniquely
 identified if Org Mobile flags them for further action.  To avoid
 setting properties configure the variable
 ~org-mobile-force-id-on-agenda-items~ to ~nil~.  Org mode then relies
 on outline paths, assuming they are unique.
 
-[fn:153] Checksums are stored automatically in the file
+[fn:154] Checksums are stored automatically in the file
 =checksums.dat=.
 
-[fn:154] The file will be empty after this operation.
+[fn:155] The file will be empty after this operation.
 
-[fn:155] https://www.ctan.org/pkg/comment
+[fn:156] https://www.ctan.org/pkg/comment
 
-[fn:156] By default this works only for LaTeX, HTML, and Texinfo.
+[fn:157] By default this works only for LaTeX, HTML, and Texinfo.
 Configure the variable ~orgtbl-radio-table-templates~ to install
 templates for other modes.
 
-[fn:157] If the =TBLFM= keyword contains an odd number of dollar
+[fn:158] If the =TBLFM= keyword contains an odd number of dollar
 characters, this may cause problems with Font Lock in LaTeX mode.  As
 shown in the example you can fix this by adding an extra line inside
 the =comment= environment that is used to balance the dollar
@@ -21759,9 +21763,9 @@ expressions.  If you are using AUCTeX with the font-latex library,
 a much better solution is to add the =comment= environment to the
 variable ~LaTeX-verbatim-environments~.
 
-[fn:158] The ~agenda*~ view is the same as ~agenda~ except that it
+[fn:159] The ~agenda*~ view is the same as ~agenda~ except that it
 only considers /appointments/, i.e., scheduled and deadline items that
 have a time specification =[h]h:mm= in their time-stamps.
 
-[fn:159] Note that, for ~org-odd-levels-only~, a level number
+[fn:160] Note that, for ~org-odd-levels-only~, a level number
 corresponds to order in the hierarchy, not to the number of stars.

+ 9 - 0
etc/ORG-NEWS

@@ -73,6 +73,15 @@ Added the capability to display remote images inline.  Whether the
 images are actually displayed are controlled by the new option
 ~org-display-remote-inline-images~.
 
+*** New allowed value for ~org-adapt-indentation~
+
+~org-adapt-indentation~ now accepts a new value, ='headline-data=.
+
+When set to this value, Org will only adapt indentation of headline
+data lines, such as planning/clock lines and property/logbook drawers.
+Also, with this setting, =org-indent-mode= will keep these data lines
+correctly aligned with the headline above.
+
 *** Babel: new header argument to pass Java command line arguments
 
 Babel Java blocks recognize header argument =:cmdargs= and pass its

+ 2 - 2
lisp/org-clock.el

@@ -1350,7 +1350,6 @@ the default behavior."
 	  (t
 	   (insert-before-markers "\n")
 	   (backward-char 1)
-	   (org-indent-line)
 	   (when (and (save-excursion
 			(end-of-line 0)
 			(org-in-item-p)))
@@ -1375,7 +1374,8 @@ the default behavior."
 		     start-time
 		     (org-current-time org-clock-rounding-minutes t)))
 	   (setq ts (org-insert-time-stamp org-clock-start-time
-					   'with-hm 'inactive))))
+					   'with-hm 'inactive))
+	   (org-indent-line)))
 	 (move-marker org-clock-marker (point) (buffer-base-buffer))
 	 (move-marker org-clock-hd-marker
 		      (save-excursion (org-back-to-heading t) (point))

+ 17 - 3
lisp/org-indent.el

@@ -178,7 +178,10 @@ during idle time."
     (setq-local indent-tabs-mode nil)
     (setq-local org-indent--initial-marker (copy-marker 1))
     (when org-indent-mode-turns-off-org-adapt-indentation
-      (setq-local org-adapt-indentation nil))
+      ;; Don't turn off `org-adapt-indentation' when its value is
+      ;; 'headline-data, just indent headline data specially.
+      (or (eq org-adapt-indentation 'headline-data)
+	  (setq-local org-adapt-indentation nil)))
     (when org-indent-mode-turns-on-hiding-stars
       (setq-local org-hide-leading-stars-before-indent-mode
 		  org-hide-leading-stars)
@@ -207,7 +210,7 @@ during idle time."
       (setq org-indent-agent-timer
 	    (run-with-idle-timer 0.2 t #'org-indent-initialize-agent))))
    (t
-    ;; mode was turned off (or we refused to turn it on)
+    ;; Mode was turned off (or we refused to turn it on)
     (kill-local-variable 'org-adapt-indentation)
     (setq org-indent-agentized-buffers
 	  (delq (current-buffer) org-indent-agentized-buffers))
@@ -365,7 +368,18 @@ stopped."
 	      level (org-list-item-body-column (point))))
 	    ;; Regular line.
 	    (t
-	     (org-indent-set-line-properties level (current-indentation))))))))))
+	     (org-indent-set-line-properties
+	      level
+	      (current-indentation)
+	      ;; When adapt indentation is 'headline-data, use
+	      ;; `org-indent--heading-line-prefixes' for setting
+	      ;; headline data indentation.
+	      (and (eq org-adapt-indentation 'headline-data)
+		   (or (org-at-planning-p)
+		       (org-at-clock-log-p)
+		       (looking-at-p org-property-start-re)
+		       (looking-at-p org-property-end-re)
+		       (looking-at-p org-property-re))))))))))))
 
 (defun org-indent-notify-modified-headline (beg end)
   "Set `org-indent-modified-headline-flag' depending on context.

+ 38 - 9
lisp/org.el

@@ -494,6 +494,12 @@ Group 1 contains drawer's name or \"END\".")
 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
   "Regular expression matching the last line of a clock drawer.")
 
+(defconst org-logbook-drawer-re
+  (concat "^[ \t]*:LOGBOOK:[ \t]*\n"
+	  "\\(?:[ \t]*.*\\(?: .*\\)?[ \t]*\n\\)*?"
+	  "[ \t]*:END:[ \t]*$")
+  "Matches an entire LOGBOOK drawer.")
+
 (defconst org-property-drawer-re
   (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
 	  "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
@@ -1514,9 +1520,15 @@ lines to the buffer:
 (defcustom org-adapt-indentation t
   "Non-nil means adapt indentation to outline node level.
 
-When this variable is set, Org assumes that you write outlines by
-indenting text in each node to align with the headline (after the
-stars).  The following issues are influenced by this variable:
+When this variable is set to t, Org assumes that you write
+outlines by indenting text in each node to align with the
+headline (after the stars).
+
+When this variable is set to 'headline-data, only adapt the
+indentation of the data lines right below the headline, such as
+planning/clock lines and property/logbook drawers.
+
+The following issues are influenced by this variable:
 
 - The indentation is increased by one space in a demotion
   command, and decreased by one in a promotion command.  However,
@@ -1528,14 +1540,18 @@ stars).  The following issues are influenced by this variable:
   when this variable is set.  When nil, they will not be indented.
 
 - TAB indents a line relative to current level.  The lines below
-  a headline will be indented when this variable is set.
+  a headline will be indented when this variable is set to t.
 
 Note that this is all about true indentation, by adding and
 removing space characters.  See also \"org-indent.el\" which does
 level-dependent indentation in a virtual way, i.e. at display
 time in Emacs."
   :group 'org-edit-structure
-  :type 'boolean
+  :type '(choice
+	  (const :tag "Adapt indentation for all lines" t)
+	  (const :tag "Adapt indentation for headline data lines"
+		 'headline-data)
+	  (const :tag "Do not adapt indentation at all" nil))
   :safe #'booleanp)
 
 (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
@@ -7237,8 +7253,13 @@ Assume point is at a heading or an inlinetask beginning."
      (goto-char (match-end 0))
      (forward-line)
      (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
+   (when (looking-at org-logbook-drawer-re)
+     (goto-char (match-end 0))
+     (forward-line)
+     (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
    (catch 'no-shift
-     (when (zerop diff) (throw 'no-shift nil))
+     (when (or (zerop diff) (not (eq org-adapt-indentation t)))
+       (throw 'no-shift nil))
      ;; If DIFF is negative, first check if a shift is possible at all
      ;; (e.g., it doesn't break structure).  This can only happen if
      ;; some contents are not properly indented.
@@ -18438,6 +18459,11 @@ ELEMENT."
 	 (t
 	  (goto-char start)
 	  (current-indentation))))
+      ((and
+	(eq org-adapt-indentation 'headline-data)
+	(memq type '(planning clock node-property property-drawer drawer)))
+       (org--get-expected-indentation
+	(org-element-property :parent element) t))
       ((memq type '(headline inlinetask nil))
        (if (org-match-line "[ \t]*$")
 	   (org--get-expected-indentation element t)
@@ -18451,7 +18477,8 @@ ELEMENT."
       ;; At first line: indent according to previous sibling, if any,
       ;; ignoring footnote definitions and inline tasks, or parent's
       ;; contents.
-      ((= (line-beginning-position) start)
+      ((and ( = (line-beginning-position) start)
+	    (eq org-adapt-indentation t))
        (catch 'exit
 	 (while t
 	   (if (= (point-min) start) (throw 'exit 0)
@@ -18476,7 +18503,7 @@ ELEMENT."
 			    (org--get-expected-indentation
 			     (org-element-property :parent previous) t))))))))))
       ;; Otherwise, move to the first non-blank line above.
-      (t
+      ((not (eq org-adapt-indentation 'headline-data))
        (beginning-of-line)
        (let ((pos (point)))
 	 (skip-chars-backward " \r\t\n")
@@ -18518,7 +18545,9 @@ ELEMENT."
 	     (goto-char start)
 	     (current-indentation)))
 	  ;; In any other case, indent like the current line.
-	  (t (current-indentation)))))))))
+	  (t (current-indentation)))))
+      ;; Finally, no indentation is needed, fall back to 0.
+      (t 0)))))
 
 (defun org--align-node-property ()
   "Align node property at point.