3

My setup

GNU Emacs GUI (version 24.4.51.1 (x86_64-w64-mingw32) compiled from Harroogan Emacs.)

Situation

I would like to type a word in PHP-mode, and it will automatically expand to a proper word.

For example, I would like to type $session and he will expand it to $_SESSION.

When reading the manual about Abbrev mode

Type the word you want to use as expansion, and then type ‘C-x a g’ and the abbreviation for it. Example:

    t h e C-x a g t e h RET

Unfortunately, if you wanted to abbreviate Read The Fine Manual with RTFM, you couldn’t just type C-x a g after “Read the Fine Manual

So I put the cursor behind the word $_SESSION, then C-x a g and type $session. But I notice it will only expand session to SESSION without the underhyphen or the dollar variable.

When I'm looking in the .abbrev_defs with M-x edit-abbrevs, then the following is mentioned:

(global-abbrev-table)

"session"    0    "SESSION"

Then I try to change it to

"session"      2    "$_SESSION"

And save it with M-x write-abbrev-file, it didn't help. Maybe the abbrev uses regexp (?), thuis excluding the dollar sign. Any another suggestion?

ReneFroger
  • 3,855
  • 22
  • 63
  • Putting `(define-abbrev-table 'global-abbrev-table '( ("session" "$_SESSION" nil 2) )) ` in `.emacs` works for me. – Name Jul 18 '15 at 04:14
  • you might use 4se for the abbrev. Number by default is accepted. This way, saves much typing, and avoids complexity altogether. Another advantage is that it avoids unwanted expansion. Disadvantage is that only you will know about the abbrev. – Xah Lee Jul 19 '15 at 09:33
  • @Name, it didn't worked for me. – ReneFroger Jul 19 '15 at 13:37
  • @XahLee thanks for the tip, however, I couldn't find anything with `4se` for abbrev. Do you have some link of something? – ReneFroger Jul 19 '15 at 13:38
  • 1
    @ReneFrogertjuh i mean, define an abbrev so that when you type 4se, it expands to what you want. – Xah Lee Jul 19 '15 at 20:38

2 Answers2

4

when you define a abbrev table, you can add a property to specify regex on what chars to look for.

Example:

(define-abbrev-table 'xah-elisp-abbrev-table
  '(
    ("d" "(defun f▮ ()\n  \"DOCSTRING\"\n  (interactive)\n  (let (VAR)\n\n  ))" nil :system t)
    ("i" "(insert ▮)" nil :system t)
    ("l" "(let (x▮)\n x\n)" nil :system t)
    ("m" "(message \"%s▮\" ARGS)" nil :system t)
    ("p" "(point)" nil :system t)
    ("s" "(setq ▮ VAL)" nil :system t)
    ("w" "(when ▮)" nil :system t)
    )

  "abbrev table for `xah-elisp-mode'"
  :regexp "\\([_-0-9A-Za-z]+\\)"
  :case-fixed t
  :enable-function 'xah-elisp-abbrev-enable-function
  )

The above is taken from a mode. I haven't tried this with normal abbrev, but am pretty sure you can do so. See a example here: http://ergoemacs.org/emacs/emacs_abbrev_mode.html

Xah Lee
  • 1,756
  • 12
  • 11
0

Get the following entry in abbrev-file:

("ds" "$_session" nil 1)

Expands as expected.

Unfortunatly this only works with my tweaked abbrev add facility.

See ar-abbrev-extensions.el at

https://github.com/andreas-roehler/werkstatt

Andreas Röhler
  • 1,894
  • 10
  • 10
  • Then I will check it out, en report it back to you. Thanks for your reply, though. – ReneFroger Jul 20 '15 at 13:19
  • I have àr-abbrev-extension.el` loaded in my init.el. But where do I put the `("ds" "$_session" nil 1)`? I couldn't figure it out. How could I get this working? – ReneFroger Jul 21 '15 at 08:13
  • @ReneFroger Defined behind the expected expansion: M-x ar-mode-abbrev-propose RET – Andreas Röhler Jul 21 '15 at 09:48
  • Sorry, it's not working for me. I typed `("$session" "$_session" nil 1)` into the minibuffer, after calling `M-x ar-mode-abbrev-propose`. It didn't expand it to `$_SESSION` in PHP-mode when I'm typing `$session` – ReneFroger Jul 21 '15 at 19:26
  • @ReneFroger The expansion might contain any character, the abbrev only word-syntax chars. "$" probably is not word-syntax. Defining "session" as abbrev of "$_session" should work. – Andreas Röhler Jul 22 '15 at 06:00
  • That's the problem. I'm sometimes using the word `session`. Therefore I would like to prefer the abbrevation `$session` to expand to `$_SESSION`. But thanks for your help anyway. :-) – ReneFroger Jul 22 '15 at 11:47
  • @ReneFroger abbrev-expansion uses backward-word. Replace that by a form travelling backward symbol-characters for example. Load the tweaked file from init. At least thats a direction how to proceed... – Andreas Röhler Jul 22 '15 at 18:32