xenodium / company-org-block

GNU General Public License v3.0
133 stars 5 forks source link

Trigger key `<` and `electric-pair-mode` #5

Closed joostkremers closed 3 years ago

joostkremers commented 3 years ago

This looks like a very useful package, I just have one little nit: in an Org buffer, pressing < inserts an additional >, because I have electric-pair-mode turned on. Completion and editing of the source block still work perfectly, but the < character is left in the buffer, which means I need to delete it manually.

Is there something that could be done about this? I know nothing about the internals of company-mode or of electric-pair-mode, so I don't know if there is a way to inhibit the insertion of the closing >. Perhaps you could check for > following < and delete it if electric-pair-mode is turned on? Or perhaps less brittle, make the trigger key customisable?

xenodium commented 3 years ago

Thanks for reporting Joost.

in an Org buffer, pressing < inserts an additional >

Quick search on electric pair, gave me this, but not sure if that works. I've not drawn much benefit from using automatic insertion of < pair in smartparens (while in org mode), so it's not enabled.

Is there something that could be done about this?

We could add a hook for some post-processing (so it's user-configurable), but maybe we can try the above first, or read on for changing the trigger.

Or perhaps less brittle, make the trigger key customisable?

This is possible now, if you override the regular expression. For example, for using the % trigger:

(setq company-org-block--regexp "%\\([^ ]*\\)")

I can expose and document this variable if you reckon this is useful? Could you try it?

I can think about the post-processing a little more.

xenodium commented 3 years ago

On second thoght, deleting the > is likely ok as the trigger is not currently configurable. Added in e2742de.

If you'd like to try the change out before it propagates to melpa, evaluate this function:

(defun company-org-block--expand (insertion)
  "Replace INSERTION with generated source block."
  (delete-region (point) (- (point) (1+ ;; Include "<" in length.
                                     (length insertion))))
  ;; If < trigger generated a matching >, delete it.
  (when (looking-at ">")
    (delete-char 1))
  (if (company-org-block--template-p insertion)
      (company-org-block--wrap-point insertion
                                     ;; May be multiple words.
                                     ;; Take the first one.
                                     (nth 0 (split-string insertion)))
    (company-org-block--wrap-point (format "src %s%s"
                                           insertion
                                           (if company-org-block-explicit-lang-defaults
                                               (company-org-block--lang-header-defaults insertion)
                                             ""))
                                   "src")))

Please close if this works for ya.

joostkremers commented 3 years ago

Please close if this works for ya.

It works, and I do think it's useful, but I just noticed it won't solve all of my problems. :slightly_smiling_face: I also have a customised input method which uses < for a number of things, and when it's on, company isn't triggered when I type <.

So I'll use your suggestion and set company-org-block--regexp, which seems to work fine. (I realise it's an internal variable, so I won't expect it to never break. :wink:)