1

Absolute beginner with Emacs and Emacs Lisp. I am using spacemacs and have been trying to get autocompletion of matching delimiters commonly used in LaTeX in Org-mode. My requirements are just getting the matching delimiters autocompleted. This is different from this question but more similar to this one.

  • when I type \( and press TAB, it should autocomplete it to \( | \) and leave the cursor at the position where | is.
  • Similarly, when I type \[ and press TAB, it should autocomplete to \[ | \] and leave the cursor at the position where | is.
1

You get exactly the behavior you describe if you put the following code in your init file. It re-binds the tab key to a new command org-cycle-or-complete-latex. At a tab keystroke org-cycle-or-complete-latex looks first whether point is behind \( or \[ and completes the math equation in this case otherwise it just runs org-cycle -- the normal binding of tab in org-mode.

(defun org-cycle-or-complete-latex (&optional arg)
  "Like `org-cycle' (which see) but completes latex math equations
if point is after \[ or \(."
  (interactive "P")
  (if (looking-back "\\\\[[(]" (max (point-min) (- (point) 2)))
      (progn
    (insert
     (case (char-before)
       (?\( "  \\)")
       (?\[ "  \\]")
       (t (error "Should never happen."))))
    (backward-char 3))
    (call-interactively #'org-cycle)))

(defun my-org-mode-hook-fun ()
  (org-defkey org-mode-map [(tab)] #'org-cycle-or-complete-latex))

(add-hook 'org-mode-hook #'my-org-mode-hook-fun)
  • Why not put that mechanism in org-mode pcomplete ? – stardiviner Dec 18 '17 at 0:49
  • @startdiviner Don't you have to press M-tab for pcomplete instead of tab? – Tobias Dec 18 '17 at 4:30
  • I use company to complete in org-mode. And org provide some place pcomplete. – stardiviner Dec 18 '17 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.