all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Marc Mientki <mientki@nonet.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Visitor here is a gedit user; how to interest him in Emacs?
Date: Thu, 05 Aug 2010 16:46:08 +0200	[thread overview]
Message-ID: <i3eirg$utm$3@news.onet.pl> (raw)
In-Reply-To: <877hk5w737.fsf@kuiper.lan.informatimago.com>

Am 05.08.2010 14:57, schrieb Pascal J. Bourguignon:
> Marc Mientki<mientki@nonet.com>  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 <tab>
-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 <tab>
%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



  reply	other threads:[~2010-08-05 14:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-05  6:12 Visitor here is a gedit user; how to interest him in Emacs? David Combs
2010-08-05  7:45 ` Elena
2010-08-05  7:50 ` Marc Mientki
2010-08-05 17:23   ` David Combs
2010-08-05 17:46     ` David Kastrup
2010-08-05  8:06 ` Marc Mientki
2010-08-05 12:57   ` Pascal J. Bourguignon
2010-08-05 14:46     ` Marc Mientki [this message]
2010-08-05  8:25 ` Elena
2010-08-05 14:55 ` despen
2010-08-06  5:49 ` TheFlyingDutchman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='i3eirg$utm$3@news.onet.pl' \
    --to=mientki@nonet.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.