Rasmus <ras...@gmx.us> writes:

> Attached is a new version of the patch that will respect the variables
> that also govern `org-insert-headline'.  It's smarter and preserves the
> layout better.

Thanks for the patch. However there are already mechanisms to complete
keywords faster (e.g., M-TAB, yasnippet). Of course, yours is more
efficient, but also very specific. It makes sense on very few keywords.
I have no strong opinion here, but I don't think it is general enough to
go into core.

Moreover, it can get in the way of expected M-RET behaviour, as in the
following example

  - item

  | #+caption: test

where M-RET is expected to insert an item.

Anyway, some comments follow.

> +      (point-at-bol-p (looking-back "^[[:space:]]*"))

  (point-at-bol-p (save-excursion (skip-chars-backward " \t") (bolp)))

See last paragraph in `looking-back' docstring.

> +      (end-of-keyword
> +       (save-excursion
> +         (beginning-of-line)
> +         (search-forward-regexp

Nitpick: `re-search-forward'

> +          org-element--affiliated-re (point-at-eol) t)

Nitpick: `line-end-position'

Also, you're not supposed to use `org-element--affiliated-re', as
suggested by the double hyphen. If you want to tell when point is at an
affiliated keyword, use

  (< (point) (org-element-property :post-affiliated element))

where ELEMENT is returned by `org-element-at-point'. You can then
extract its name with, e.g.,

   (looking-at "#\\+\\(.+?\\):")
   (upcase (org-match-string-no-properties 1)))
> +      (elm (org-element-at-point))
> +      (key (and (eq 'keyword (org-element-type elm))
> +                (org-element-property :key elm))))
> +    (when key

KEY is nil when at an affiliated keyword. So this function is a no-op
on #+CAPTION: and alike.  Try it on a real caption, e.g.,

  #+CAPTION: caption

> +      (when point-at-bol-p (open-line 1)
> +         ;; Open-line makes sometimes ruins indention of the
> +         ;; previous line.
> +         (save-excursion (forward-line 1)
> +                         (org-indent-line)))

Don't use `open-line' at all. Insert "\n" where appropriate. Also avoid
using `org-indent-line' since you can already know what the expected
indentation is (i.e., by storing it earlier).

> +      (unless may-split (end-of-line))
> +      (unless point-at-bol-p
> +     (when (< (point) end-of-keyword)
> +       (goto-char end-of-keyword))
> +     (insert "\n"))
> +      (insert (format "#+%s: " key))
> +      (org-indent-line))))

Ditto. Use stored indentation.

> +            ((and (eq type 'keyword)
> +                  ;; Keyword such as LATEX, ATTR_LATEX, CAPTION, and HEADER,
> +                  ;; LATEX_HEADER, LATEX etc. can occur multiple times.
> +                  (let ((key (org-element-property :key element)))
> +                    (if (member key org-element-affiliated-keywords)
> +                        (member key org-element-multiple-keywords)
> +                      t)))

KEY cannot belong to `org-element-affiliated-keywords'.  See above.


  (or (not (member key org-element-affiliated-keywords))
      (member key org-element-multiple-keywords))


Nicolas Goaziou

Reply via email to