all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* manipulating abbreviation mode hooks
@ 2010-05-10  1:38 Suvayu Ali
  2010-05-16 12:29 ` Suvayu Ali
  0 siblings, 1 reply; 3+ messages in thread
From: Suvayu Ali @ 2010-05-10  1:38 UTC (permalink / raw
  To: Emacs mailing list

Hi everyone,

I'm not sure I'll be able to explain what I am trying to do but I'll 
give it a shot nonetheless. I wrote some abbreviations for the c++ mode 
(e.g. `for' expands to the entire for loop construct, if..else, ...). 
However when ever I use any of those abbrevs within a comment, it still 
expands to the loop.

 From the info node, "(elisp)Top:: > *Note Abbrevs:: > Abbrev Expansion" 
I got the hint to use `abbrev-expand-functions' hook. To start out 
simple, I tried to implement this for shell-script mode abbrev table, 
and the text-mode abbrev table.

So in a scratch buffer I tried this,

> (defun foo ()
>   (interactive)
>   (if (not (save-excursion
> 	     (and
> 	      (forward-line 0)
> 	      (eq (char-after) ?#))))
>       (print "src")
>     (let ((local-abbrev-table text-mode-abbrev-table))
>       (print "comments")
>       (funcall 'expand-abbrev))
>     ))

This worked as expected, my text mode abbreviations were expanded on a 
line starting with #, but didn't expand otherwise.

But when I put the following in my ~/.emacs, I get no such behaviour.

> (defun my-shell-script-mode-abbrev-expand-function ()
>   (if (not (save-excursion
> 	     (forward-line 0)
> 	     (eq (char-after) ?#)))
>       (funcall 'expand-abbrev)
>     (let ((local-abbrev-table text-mode-abbrev-table))
>       (funcall 'expand-abbrev))))
>
> (add-hook 'shell-script-mode-hook
> 	  '(lambda ()
> 	     (add-hook 'abbrev-expand-functions
> 		       'my-shell-script-mode-abbrev-expand-function
> 		       nil t)))


Once this is solved, my second question is how do I implement this for 
c++-mode? I can't figure out how to match to `//' or a `/* ... */' 
block. I'm a newbie in lisp, so a little guidance would be very 
appreciated. :)

TIA
-- 
Suvayu

Open source is the future. It sets us free.




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: manipulating abbreviation mode hooks
       [not found] <mailman.16.1273455544.17813.help-gnu-emacs@gnu.org>
@ 2010-05-10  9:34 ` Andreas Politz
  0 siblings, 0 replies; 3+ messages in thread
From: Andreas Politz @ 2010-05-10  9:34 UTC (permalink / raw
  To: help-gnu-emacs

Suvayu Ali <fatkasuvayu+linux@gmail.com> writes:

[...]
>
> But when I put the following in my ~/.emacs, I get no such behaviour.
>
>> (defun my-shell-script-mode-abbrev-expand-function ()
>>   (if (not (save-excursion
>> 	     (forward-line 0)
>> 	     (eq (char-after) ?#)))
>>       (funcall 'expand-abbrev)
>>     (let ((local-abbrev-table text-mode-abbrev-table))
>>       (funcall 'expand-abbrev))))
>>
>> (add-hook 'shell-script-mode-hook
>> 	  '(lambda ()
>> 	     (add-hook 'abbrev-expand-functions
>> 		       'my-shell-script-mode-abbrev-expand-function
>> 		       nil t)))
>
>
> Once this is solved, my second question is how do I implement this for
> c++-mode? I can't figure out how to match to `//' or a `/* ... */'
> block. I'm a newbie in lisp, so a little guidance would be very
> appreciated. :)
>
> TIA

shell-script-mode is an alias for sh-mode, the hook is called
sh-mode-hook.  Anyway, I think it's easier to define the
:enable-function property on your abbrev table.

(abbrev-table-put your-abbrev-table
                  :enable-function
                  (lambda nil
                    (not (nth 4 (syntax-ppss)))))

-ap


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: manipulating abbreviation mode hooks
  2010-05-10  1:38 manipulating abbreviation mode hooks Suvayu Ali
@ 2010-05-16 12:29 ` Suvayu Ali
  0 siblings, 0 replies; 3+ messages in thread
From: Suvayu Ali @ 2010-05-16 12:29 UTC (permalink / raw
  To: Emacs mailing list

Just in case someone else was looking for a solution something similar,

On Sunday 09 May 2010 06:38 PM, Suvayu Ali wrote:
> Hi everyone,
>
> I'm not sure I'll be able to explain what I am trying to do but I'll
> give it a shot nonetheless. I wrote some abbreviations for the c++ mode
> (e.g. `for' expands to the entire for loop construct, if..else, ...).
> However when ever I use any of those abbrevs within a comment, it still
> expands to the loop.
>
>  From the info node, "(elisp)Top:: > *Note Abbrevs:: > Abbrev Expansion"
> I got the hint to use `abbrev-expand-functions' hook. To start out
> simple, I tried to implement this for shell-script mode abbrev table,
> and the text-mode abbrev table.
>
> So in a scratch buffer I tried this,
>
>> (defun foo ()
>> (interactive)
>> (if (not (save-excursion
>> (and
>> (forward-line 0)
>> (eq (char-after) ?#))))
>> (print "src")
>> (let ((local-abbrev-table text-mode-abbrev-table))
>> (print "comments")
>> (funcall 'expand-abbrev))
>> ))
>
> This worked as expected, my text mode abbreviations were expanded on a
> line starting with #, but didn't expand otherwise.
>
> But when I put the following in my ~/.emacs, I get no such behaviour.
>
>> (defun my-shell-script-mode-abbrev-expand-function ()
>> (if (not (save-excursion
>> (forward-line 0)
>> (eq (char-after) ?#)))
>> (funcall 'expand-abbrev)
>> (let ((local-abbrev-table text-mode-abbrev-table))
>> (funcall 'expand-abbrev))))
>>
>> (add-hook 'shell-script-mode-hook
>> '(lambda ()
>> (add-hook 'abbrev-expand-functions
>> 'my-shell-script-mode-abbrev-expand-function
>> nil t)))
>
>
> Once this is solved, my second question is how do I implement this for
> c++-mode? I can't figure out how to match to `//' or a `/* ... */'
> block. I'm a newbie in lisp, so a little guidance would be very
> appreciated. :)
>

I solved it like this,

,----
| (defun expand-abbrev-in-context (expand)
|   "Expands abbreviations according to the context. Determines whether 
within
| comments or source by looking at the face name. If within comments the
| `text-mode-abbrev-table' is used, the major mode abbrev-table is used 
otherwise.
|
| Expansion is done by the function passed as the argument. This is 
controlled by
| the \"abnormal\" hook `abbrev-expand-functions'."
|   (if (not (save-excursion
| 	     (string-match "comment"
| 			   (symbol-name (if (< (point) (point-max))
| 					    (face-at-point)
| 					  (backward-char)
| 					  (face-at-point))))))
|       (funcall expand)
|     (let ((local-abbrev-table text-mode-abbrev-table))
|       (funcall expand))))
| (add-hook 'after-change-major-mode-hook
| 	  (lambda ()
| 	    (add-hook 'abbrev-expand-functions 'expand-abbrev-in-context nil t)))
`----

Hope this helps someone in the future.

-- 
Suvayu

Open source is the future. It sets us free.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-05-16 12:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-10  1:38 manipulating abbreviation mode hooks Suvayu Ali
2010-05-16 12:29 ` Suvayu Ali
     [not found] <mailman.16.1273455544.17813.help-gnu-emacs@gnu.org>
2010-05-10  9:34 ` Andreas Politz

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.