0

I'm a beginner Emacs user, and English isn't my first language so please bear with me.

I use Emacs for writing prose, and I need help with punctuation.

To put it short, I want to turn this:
--hello strangerRET

into this:
–Hello stranger.

I want to use dashes instead of quotations to write dialogue, either an en-dash and an em-dash. With other software, I have simply made an auto correction from two short dashes -- to a longer dash. But with Emacs an abbrev needs a space in the end, which I don't want to put there, also I haven't figured out how to make abbrevs with punctuation only.

I also need this to work with some form of automatic capitalization mode so that the mode understands the dash as the beginning of a word like it does with a quotation mark.

The reason I do it like this is the keyboard I use and the fact I have this method in my muscle memory from using it elsewhere.

Also, I'd like to get a fullstop at the end of a line if no other punctuation exists there.

One more thing, I know there is some key command that you can use to delete a selected block of text where it just sends it to the bottom of the buffer rather than killing it. Can somebody point me to it?

0

One approach is to redefine the return command. Here is a command the does what you want I think. Then, I redefine in the org-mode keymap (you could do this in some other map if you use it.

(defun dash-return (&optional ignore)
  (interactive "P")
  (if ignore
      (org-return)
    (let ((p (point)))
      (goto-char (line-beginning-position))
      (cond
       ;; not on a quote so go back and return
       ((not (looking-at "--"))
    (goto-char p))
       ;; on a quote, so 
       (t
    ;; replace -- with –
    (setf (buffer-substring (point) (+ 2 (point))) "–")
    ;; Capitalize the first word
    (capitalize-word 1)
    ;; Make sure there is a period at the end, skip spaces back t0 end.
    (goto-char (line-end-position))
    (skip-chars-backward " ")
    (when (not (looking-back "[!?.]" 1))
      (insert "."))))
      (org-return))))

(define-key org-mode-map (kbd "RET") 'dash-return)

This should act like org-return when not on a line that starts with -- I think, and you can use C-u ret to avoid the reformatting.

For the other command you mentioned, I am not familiar with one, but here is a simple way to get that:

(defun send-region-to-end (r1 r2)
  (interactive "r")
  (let ((text (buffer-substring r1 r2)))
    (delete-region r1 r2)
    (save-excursion
      (goto-char (point-max))
      (insert "\n\n" text))))
  • Hot damn! Very nice. I got it to work by starting Emacs, turning on org-mode and then avaluating the code in scratch buffer. However, if I add it to my dotfile, Emacs fails to start properly. My guess is that since org-mode isn't on when Emacs starts, the org-mode-map part fails to evaluate? I am not a programmer so most of the code is a mystery to me. Can you help? Also, is there a way to ignore the fullstop addition if there already is a ! or ? at the end of the line? The end of the buffer dump works like a charm, I'll map that to a key as soon as I can get to it. Thank you so much! – Sami Lahtinen Oct 27 at 16:14
  • I edited the function to allow !? or . to be at the end. You need to put the define-key part of that code after you have loaded org-mode. So maybe just put (require 'org) right before it. Or put this code after the place you load org-mode. – John Kitchin Oct 27 at 17:01
  • Got it to work perfectly. Would it be a bad idea to use global-set-key for this? Would it then work outside org-mode too? – Sami Lahtinen Oct 27 at 17:30
  • The main reason not to use global-set-key is just that you don't write quotes in every mode, and some modes have smarter ret settings, so you might not want to redefine it for all modes. also, here we use org-return as the default return, which is mainly relevant in org-mode. – John Kitchin Oct 27 at 21:46
  • I see. I've been playing around with the functionality now, and I'm quite happy with it. I think I will at some point look into turning this into a mode so the functionality can be turned off in case I need to do something that does not work with it. The automatic newlines from auto-fill-mode for instance seem to give erratic results, I'm using global-visual-line at the moment so that is not a problem, but something like it might be in the future. Once again, thank you. – Sami Lahtinen Oct 27 at 22:43

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.