Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YASnippet select more than field in latex mode #579

Closed
pascal-niklaus opened this issue May 17, 2015 · 11 comments · Fixed by #755
Closed

YASnippet select more than field in latex mode #579

pascal-niklaus opened this issue May 17, 2015 · 11 comments · Fixed by #755

Comments

@pascal-niklaus
Copy link

In a LaTeX snippet, when the snippet is inserted, not just "field" is selected for entering but the entire line including the leading %% (see below). For the 2nd field, this then works correctly. Looks like it is related to the '%', because adding another character after the two (or three) % fixed this behaviour.

# -*- mode: snippet; require-final-newline: nil -*-
# name: xtst
# key: xtst
# binding: direct-keybinding
# --
%%
%% ${1:field}
%%
%% ${2:field}
%%

yasissue

@npostavs
Copy link
Collaborator

Doesn't happen here. Which Emacs and Yasnippet versions are you using? Also, is this the builtin latex-mode, or are you using AUCTeX package?

@pascal-niklaus
Copy link
Author

I am using AUCTeX, package-activated-list equals (auctex auto-complete-auctex auto-complete popup yasnippet hideshow-org popup yasnippet).

Version is 20150415.244 from elpa.

@npostavs
Copy link
Collaborator

Emacs version?

Can you check if it happens with just builtin latex-mode?

@pascal-niklaus
Copy link
Author

Emacs is version 24.5.1. The problem disappears if I uninstall auctex.

@npostavs
Copy link
Collaborator

Confirmed. The markers for the field go wrong during indentation, I can't tell why.

Minimal reproduction recipe:

  • download and untar http://ftp.gnu.org/pub/gnu/auctex/auctex-11.88.tar.gz
  • ./configure --disable-preview
  • make tex-site.el
  • emacs -Q -L yasnippet/ -L auctex-11.88/ -l yasnippet.el -l tex-site.el -f yas-global-mode
  • Create a new buffer
  • M-x latex-mode
  • M-: (yas-expand-snippet "%\n% ${1:field}")

@joaotavora
Copy link
Owner

The markers for the field go wrong during indentation, I can't tell why.

Indentation happens after snippet field insertion (it may depend on field contents, for instance). The problem is that some indentation techniques destroy the whole whitespace in front of the line (or maybe the whole line, who knows?) and restore an indented version. Obviously, yasnippet's markers aren't restored by those strategies, so there is yas--indent-according-to-mode, which tries to. There is some effort there to describe the problem, though in a language that I myself found hard to understand. And the implementation seems a little bit contorted (this is quite old code...)

I don't have time to fix this right now, but if you can think of some better approach, I would very much like to hear it, as this problem is central in the yasnippet engine, which is being exported to the snippet-engine branch.

From your exchange, though, I still haven't understood if this happens with vanilla Emacs' latex-mode, though.

@joaotavora
Copy link
Owner

In yas--indent-according-to-mode, a better approach might be to take a snapshot of the current line's contents and yas-specific markers, before calling indent-according-to-mode, then call it, then finally match the snapshot against newly indented line and reset the markers from that.

@npostavs
Copy link
Collaborator

From your exchange, though, I still haven't understood if this happens with vanilla Emacs' latex-mode, though.

It doesn't.


Probably related to what AUCTeX calls "inner indentation":
http://git.savannah.gnu.org/cgit/auctex.git/tree/latex.el?id=release_11_88#n2790

;; b) Inner indentation: Indentation after the comment character(s)
;; (taking into account possible comment padding).

@npostavs
Copy link
Collaborator

The problem is that some indentation techniques destroy the whole whitespace in front of the line (or maybe the whole line, who knows?) and restore an indented version. Obviously, yasnippet's markers aren't restored by those strategies

I think it might be enough to (set-marker-insertion-type <field-start-marker> t). It looks like the markers stay in place fine when the whitespace is deleted, but if the marker happens to be sitting at the beginning, then when adding text back in, the marker stays put, while the actual field start gets pushed forward. Type t markers get pushed forward when text is inserted at their position.

It works for this case at least, need to test more to see if it works elsewhere.


I used this code to play with marker types:

(switch-to-buffer (get-buffer-create "mtest.latex"))
(erase-buffer)
(latex-mode)

(defvar m-beg (make-marker))
(defvar mt-beg (make-marker))
(defvar m-end (make-marker))

(insert "%\n% ")
(set-marker m-beg (point))
(set-marker mt-beg (point))
(insert "field")
(set-marker m-end (point))

(set-marker-insertion-type mt-beg t)


(defun show-markers ()
  (interactive)
  (message "(%d, %d): %s; (%dt, %d): %s"
           (marker-position m-beg) (marker-position m-end) (buffer-substring m-beg m-end)
           (marker-position mt-beg) (marker-position m-end) (buffer-substring mt-beg m-end)))
(define-key global-map [f12] 'show-markers)

Hit f12 to see where the markers are currently, then change the buffer text and hit f12 again to see how the markers were affected.

@joaotavora
Copy link
Owner

Probably related to what AUCTeX calls "inner indentation":

Most definitely, yes.

I think it might be enough to (set-marker-insertion-type t).

Not enough. Suppose you have "whitespace -> null field -> whitespace", for example. You are also assuming that the major-mode adds whitespace using insert at the very beginning of the line, but it might not. Later today I'll try to dig other issues that dealt with exactly this problem, I kinda remember cperl-mode being problematic

@npostavs
Copy link
Collaborator

Suppose you have "whitespace -> null field -> whitespace",

Ah, I didn't consider null fields indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants