From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marc Mientki Newsgroups: gmane.emacs.help Subject: Re: Visitor here is a gedit user; how to interest him in Emacs? Date: Thu, 05 Aug 2010 16:46:08 +0200 Organization: http://onet.pl Message-ID: References: <877hk5w737.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1291855084 25524 80.91.229.12 (9 Dec 2010 00:38:04 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 9 Dec 2010 00:38:04 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 09 01:38:00 2010 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 1PQUWF-0008QJ-7X for geh-help-gnu-emacs@m.gmane.org; Thu, 09 Dec 2010 01:37:59 +0100 Original-Received: from localhost ([127.0.0.1]:40105 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQUWE-0002jE-FG for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 19:37:58 -0500 Original-Path: usenet.stanford.edu!goblin2!goblin.stu.neva.ru!news.nask.pl!news.nask.org.pl!news.onet.pl!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 168 Original-NNTP-Posting-Host: port-83-236-194-74.static.qsc.de Original-X-Trace: news.onet.pl 1281019568 31670 83.236.194.74 (5 Aug 2010 14:46:08 GMT) Original-X-Complaints-To: niusy@onet.pl Original-NNTP-Posting-Date: Thu, 5 Aug 2010 14:46:08 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.11) Gecko/20100711 Thunderbird/3.0.6 In-Reply-To: <877hk5w737.fsf@kuiper.lan.informatimago.com> Original-Xref: usenet.stanford.edu gnu.emacs.help:180409 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:76309 Archived-At: Am 05.08.2010 14:57, schrieb Pascal J. Bourguignon: > Marc Mientki writes: > >> Am 05.08.2010 08:12, schrieb David Combs: >> >>> every time I try to show him some Emacs concep, he says >>> that he can do that in gedit also. >> >> Another example. Let him convert that >> >> value_at_1_4 = 10; >> value_at_2_2 = 11; >> value_at_5_1 = 300; >> >> to this: >> >> value[3][0] = 10; >> value[1][1] = 11; >> value[0][4] = 300; > >> So he should extract two last number parts from variable name, remove >> _at_' from name, use both index for c-array-indexing but in reverse >> order and 0-based (original was 1-based). Of course everything in one >> pass in full automatic :-) > > Even better: > > /* > > matrix is: > | 2 9 11 | > | 6 5 0 | > | 1 12 7 | > > */ > > and have it transform this comment into the matrix intialization code: > > matrix[1][1]=2; > matrix[1][2]=6; > matrix[1][3]=1; > matrix[2][1]=9; > matrix[2][2]=5; > matrix[2][3]=12; > matrix[3][1]=11; > matrix[3][2]=0; > matrix[3][3]=7; > > > > > > > (defun matrix-to-c (start end) > (interactive "r") > (goto-char start) > (when (re-search-forward "\\([_A-Za-z][_A-Za-z0-9]*\\) *is *:" end t) > (let ((name (match-string 1)) > (data '())) > (while (re-search-forward "|\\(.*\\)|" end t) > (push (first (read-from-string (concat "(" (match-string 1) ")"))) data)) > (setf data (reverse data)) > (search-forward "*/") > (goto-char (match-end 0)) > (insert "\n") > (loop > for j from 1 to (length data) > do (loop > for i from 1 to (length (first data)) > do (insert (format "%s[%s][%s]=%s;\n" name j i (elt (elt data (1- i)) (1- j))))))))) > Very fine example! Normaly I prefere to write small piece elisp code to solve such task, too. But sometimes I find a puzzle with macros very funny, especialy in non trivial cases like this one. So I wrote matrix-to-c as macro. With some limitations it works fine. First limitation: I expect matrix form without '|': 2 9 11 6 5 0 1 12 7 Second: I use genial macro-math.el from Nikolaj Schumacher with following key bindings: (global-set-key "\C-x~" 'macro-math-eval-and-round-region) (global-set-key "\C-x=" 'macro-math-eval-region) This is my solution: M-C-s ;; isearch-forward-regexp [0-9] ;; self-insert-command * 5 RET ;; newline-and-indent C-a ;; move-beginning-of-line TAB ;; c-indent-line-or-region C-M-SPC ;; mark-sexp C-x r s ;; copy-to-register 1 ;; self-insert-command C-w ;; kill-region matrix[ ;; self-insert-command * 7 C-SPC ;; set-mark-command ( ;; c-electric-paren C-x C-k -1 ;; self-insert-command * 2 ) ;; c-electric-paren / ;; c-electric-slash 3 ;; self-insert-command C-u 0 C-x ~ ;; macro-math-eval-and-round-region ][ ;; self-insert-command * 2 C-SPC ;; set-mark-command C-u C-x C-k %3 ;; self-insert-command * 2 C-x = ;; macro-math-eval-region ] ;; self-insert-command SPC ;; self-insert-command = ;; self-insert-command SPC ;; self-insert-command C-u C-x r i ;; insert-register 1 ;; self-insert-command ; ;; c-electric-semi&comma RET ;; newline-and-indent Or ready to use: (fset 'matrix-to-c [?\C-\M-s ?\[ ?0 ?- ?9 ?\] ?\C-m ?\C-a ?\C-i ?\C-\M- ?\C-x ?r ?s ?1 ?\C-w ?m ?a ?t ?r ?i ?x ?\[ ?\C- ?\( ?\C-x ?\C-k tab ?- ?1 ?\) ?/ ?3 ?\C-u ?0 ?\C-x ?~ ?\] ?\[ ?\C- ?\C-u ?\C-x ?\C-k tab ?% ?3 ?\C-x ?= ?\] ? ?= ? ?\C-u ?\C-x ?r ?i ?1 ?\; ?\C-m]) So now one can paste matrix form into C code: 2 9 11 6 5 0 1 12 7 init emacs macro counter to 0 (C-x C-k C-c 0 RET) and then call 9 times matrix-to-c: C-u 9 M-x matrix-to-c and we get: matrix[0][0] = 2; matrix[0][1] = 9; matrix[0][2] = 11; matrix[1][0] = 6; matrix[1][1] = 5; matrix[1][2] = 0; matrix[2][0] = 1; matrix[2][1] = 12; matrix[2][2] = 7; (I used first index as row, second as col, 0-based). I love Emacs! :-) regards Marc