From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Suvayu Ali Newsgroups: gmane.emacs.help Subject: Re: manipulating abbreviation mode hooks Date: Sun, 16 May 2010 05:29:50 -0700 Message-ID: <4BEFE53E.9070200@gmail.com> References: <4BE763A7.20100@gmail.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1274013029 20036 80.91.229.12 (16 May 2010 12:30:29 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sun, 16 May 2010 12:30:29 +0000 (UTC) To: Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun May 16 14:30:28 2010 connect(): No such file or directory Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ODczE-0000NM-2f for geh-help-gnu-emacs@m.gmane.org; Sun, 16 May 2010 14:30:28 +0200 Original-Received: from localhost ([127.0.0.1]:39904 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ODczD-0008Sx-Eo for geh-help-gnu-emacs@m.gmane.org; Sun, 16 May 2010 08:30:27 -0400 Original-Received: from [140.186.70.92] (port=51163 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ODcyh-0008Qu-VQ for help-gnu-emacs@gnu.org; Sun, 16 May 2010 08:29:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1ODcyf-0004JU-Su for help-gnu-emacs@gnu.org; Sun, 16 May 2010 08:29:55 -0400 Original-Received: from mail-gy0-f169.google.com ([209.85.160.169]:52809) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1ODcyf-0004JI-Pz for help-gnu-emacs@gnu.org; Sun, 16 May 2010 08:29:53 -0400 Original-Received: by gyg4 with SMTP id 4so2759223gyg.0 for ; Sun, 16 May 2010 05:29:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:message-id:date:from :user-agent:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=r5aypXLoP1fZRDo/QKgf4i8sgAOzfDuqX/Sg5C7Oj/4=; b=QuLr5YdAiD1xGc/mR/rHKG+ZAYju63LvFFBjnwKWU6qmT+7G8EQY8JGYljzlXli/1s BvHMS0yi830uRmQ26qmasFI+e+v3geXeUR/BDmyBj8w7bmxUPb77/QjRsl+Cu4EHHA9t FWi5o9l0Orkhc5IMBirVi7U4qpQrDBtXl7l/g= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; b=s7hRWi/wqgb+TYhHIVkUmox4gBJEih94UEzdRnpTxRzZA8ELqzprS9mu1VWPJL9A3w sNnqX8WZvnp/XBDh7FowqUpHvULjAYZqSd5B79r63yT0/JI/mkjzMXjBnLJ6x7rDxzaZ vnvkLte5l9gEEupzFR+0VWbvksWFkHJ3JLcEA= Original-Received: by 10.101.158.19 with SMTP id k19mr4583957ano.220.1274012992543; Sun, 16 May 2010 05:29:52 -0700 (PDT) Original-Received: from bhishma.homelinux.net (d207-6-164-45.bchsia.telus.net [207.6.164.45]) by mx.google.com with ESMTPS id 13sm2427667gxk.12.2010.05.16.05.29.51 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 16 May 2010 05:29:52 -0700 (PDT) User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100330 Fedora/3.0.4-1.fc11 Lightning/1.0b2pre Thunderbird/3.0.4 In-Reply-To: <4BE763A7.20100@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:73660 Archived-At: 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.