Redefining “sentence” in Emacs? (single space between sentences, but ignoring abbreviations)

前端 未结 1 1092
花落未央
花落未央 2021-02-13 01:38

I would like to be able to navigate by sentence in Emacs (M-a, M-e). Here\'s the problem: by default, Emacs expects that each sentence is separated by two spaces, and I\'m used

相关标签:
1条回答
  • 2021-02-13 02:41

    I don't think sentence-end will do what you need it to do. You really need look-ahead regexps for this, and Emacs doesn't support them.

    You can roll your own function to do what you need though. I don't understand all of your requirements, but the following is a start:

    (defun my-next-sentence ()
    "Move point forward to the next sentence.
    Start by moving to the next period, question mark or exclamation.
    If this punctuation is followed by one or more whitespace
    characters followed by a capital letter, or a '\', stop there. If
    not, assume we're at an abbreviation of some sort and move to the
    next potential sentence end"
      (interactive)
      (re-search-forward "[.?!]")
      (if (looking-at "[    \n]+[A-Z]\\|\\\\")
          nil
        (my-next-sentence)))
    
    (defun my-last-sentence ()
      (interactive)
      (re-search-backward "[.?!][   \n]+[A-Z]\\|\\.\\\\" nil t)
      (forward-char))
    

    Most of your tweaking will need to focus on the looking-at regexp, to make sure it hits all the potential end-of-sentence conditions you need. It would be relatively easy to modify it to move the cursor to particular locations based on what it finds: Leave it be if it's a normal sentence, move past the next { if you're at a latex command, or whatever suits you.

    Once you've got that working, can bind the functions to a M-a and M-e, probably using mode-hooks unless you want to use them for every mode.

    0 讨论(0)
提交回复
热议问题