help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Visitor here is a gedit user; how to interest him in Emacs?


From: Marc Mientki
Subject: Re: Visitor here is a gedit user; how to interest him in Emacs?
Date: Wed, 08 Dec 2010 15:23:10 -0000
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.11) Gecko/20100711 Thunderbird/3.0.6

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 via email to

[Prev in Thread] Current Thread [Next in Thread]