You are not logged in.

#1 2010-09-28 17:48:03

alexandre
Member
Registered: 2010-07-17
Posts: 6

Replacing a string of character by another one with emacs

Hello,

With Vim, I had something like this in my setup (tex.vim in ftplugin)

imap <buffer> ... \ldots

imap <buffer> « \og
imap <buffer> » \fg

imap <buffer> " \textquotedblleft 
imap <buffer> ¢ \textquotedblright\ 


imap <buffer> € \EUR
imap <buffer>~ $\sim\ 

imap <buffer> // \\
imap <buffer> /np \newpage

Currently, I am trying emacs, and would like the same setup. I have searched the web, but with no luck. Indeed, I've just found the following code (and made some modifications to try it) :

(defun replace-string-pairs-region (start end mylist)
  "Replace string pairs in region."
  (save-restriction 
    (narrow-to-region start end)
    (mapc
      (lambda (arg)
        (goto-char (point-min)) 
        (while (search-forward (car arg) nil t) (replace-match (cadr arg)) )
      ) mylist
    )
  )
)

(defun replace-chars (start end)
  "Replace "..." by "\ldots" and other similar chars"
  (interactive "r")
(replace-string-pairs-region start end '(
("..." "\ldots")
;("<" "&lt;")
;(">" "&gt;")
    )
  )
)

But it doesn't work.

Does anyone know how could I replace "..." by "\ldots" and other things like that with Emacs ?

Last edited by alexandre (2010-09-28 17:48:50)

Offline

#2 2010-09-29 12:39:58

return new int[5]
Member
Registered: 2010-09-29
Posts: 8

Re: Replacing a string of character by another one with emacs

Press Ctrl+Alt+%
Let it search for \.\.\., because a regular point is seen as one character.
Replace with \\ldots, same story with \
Press ! to replace all occurencies.

Offline

#3 2010-09-30 12:27:00

alexandre
Member
Registered: 2010-07-17
Posts: 6

Re: Replacing a string of character by another one with emacs

Thank you a lot for your answer, but my problem was not to replace "..." by "\ldots" after typing it, but how could I replace "..." by "\ldots" while I am typing it, just after I finished to push the last dot.

I have tried to set it up with the abbrev mode, but it seems to work only for words. So for example, if I enter the word such as, say, "arch", then it replace it automatically by "archlinux" after pressing the space key. I won't have to replace the string manually, it does it automatically with the abbreviation mode of Emacs. Here is an example of what I am talking about :

 (abbrev-mode t) ; completion automatique
 (global-set-key (quote [tab]) (quote dabbrev-expand))

    (setq default-abbrev-mode t)
  (define-abbrev-table 'global-abbrev-table '(
     ("..." "\ldots" t)
     ("arch" "archlinux" nil)
     ))

But my problem is, it doesn't work for "...", just for words. Of course, I could do it manually with the shortcut you wrote, but it would be nice to make it work automatically. Is there a way to do so ?

If you don't understand what I am talking  about, try Vim, copy "imap <buffer> ... \ldots" in your .vimrc, and Vim will replace all "..." by "\ldots" while typing. I am sure I can do it with Emacs, I just don't know how.

Last edited by alexandre (2010-09-30 12:28:38)

Offline

#4 2010-09-30 14:22:50

return new int[5]
Member
Registered: 2010-09-29
Posts: 8

Re: Replacing a string of character by another one with emacs

Yes, I understand what you're saying. I have no idea if that's possible, but as you said, you can type the whole text and replace it afterwards. I'd suggest searching some more in the Info of emacs.  (F1 + i)

Offline

#5 2010-09-30 23:13:21

alexandre
Member
Registered: 2010-07-17
Posts: 6

Re: Replacing a string of character by another one with emacs

Ok, thank you, I will continue to search.

Offline

#6 2010-10-02 06:35:22

jdd
Member
From: OH, USA
Registered: 2009-03-03
Posts: 11
Website

Re: Replacing a string of character by another one with emacs

in psuedo python (my elisp is a little rusty...)

regex = '...'
start = regex.tokens[0]

function check(index):
  unbind currently bound keys
  if index == regex.tokens.length:
    replace the string
  if regex.tokens[index].text(char-at-point):
    bind all keys that match regex.tokens[index] to:
      check(index +1)

bind all keys that match start to check(0)

This is possible, but it's probably a bit hefty, I'm sure there's an easier way.

Hmm, just did a quick search -- check C-h v dabbrev-abbrev-char-regexp . Looks like you should just be able to give it a regexp that also matches literal dots.

Last edited by jdd (2010-10-02 06:35:47)

Offline

#7 2010-10-02 13:36:09

juster
Forum Fellow
Registered: 2008-10-07
Posts: 195

Re: Replacing a string of character by another one with emacs

I searched for 'emacs ldots' and found this latex mode hook: http://www.columbia.edu/~jr2075/elisp/ldots.el

You can actually rebind the '.' key to check if it is the third period and to delete these periods and insert '\ldots' if this is true. Sorry I don't have time right now to write my own elisp test for this.

I don't think abbrev-mode can help you because it looks only at word characters (it uses the word functions like `backward-word`, etc). From a quick look at the info page it looks like dynamic abbrev mode is not automatic.

Offline

Board footer

Powered by FluxBB