Announcements and discussions for Gnus, the GNU Emacs Usenet newsreader
 help / color / mirror / Atom feed
* need help on hobby project
@ 2015-01-16 19:29 Hugh Lawson
  2015-01-16 21:01 ` Hugh Lawson
  2015-01-16 21:44 ` Emanuel Berg
  0 siblings, 2 replies; 3+ messages in thread
From: Hugh Lawson @ 2015-01-16 19:29 UTC (permalink / raw)
  To: info-gnus-english


To improve my scanty knowledge of elisp, I want to rewrite a macro as a
function. I can't get to first base on this.  I think I can do this if
given links or keywords for the self-instruction of a noob.

For example I can't make out this:  

(shell-command COMMAND &optional OUTPUT-BUFFER ERROR-BUFFER)

I can't find a reference on how to read this line.

The function  should do this:

I put point  on a word in text buffer.  Then I hit a command
key, say C-l.

2.command key calls function that does this:

          reads word into an accessible place as a string

          shell-command "whitaker <put word string here>"

          latin calls my latin-english dictionary

          the output appears in the shell command buffer

3.  I accomplised this with this  keyboard macro:

 

;;look up latin words
(fset 'whitaker
   [?\M-x ?c ?o ?p ?y ?- ?w ?o tab return ?\M-x ?s ?h ?e ?l tab ?- ?c ?o ?m ?m tab return ?l ?a ?t ?i ?n ?  ?\C-y return])


(global-set-key (kbd "C-l") 'whitaker)
          

The macro uses the function copy-word, borrowed from here:


http://www.emacswiki.org/emacs/CopyWithoutSelection

Thanks.

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

* Re: need help on hobby project
  2015-01-16 19:29 need help on hobby project Hugh Lawson
@ 2015-01-16 21:01 ` Hugh Lawson
  2015-01-16 21:44 ` Emanuel Berg
  1 sibling, 0 replies; 3+ messages in thread
From: Hugh Lawson @ 2015-01-16 21:01 UTC (permalink / raw)
  To: info-gnus-english

Ooops! wrong group, sorry about that.

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

* Re: need help on hobby project
  2015-01-16 19:29 need help on hobby project Hugh Lawson
  2015-01-16 21:01 ` Hugh Lawson
@ 2015-01-16 21:44 ` Emanuel Berg
  1 sibling, 0 replies; 3+ messages in thread
From: Emanuel Berg @ 2015-01-16 21:44 UTC (permalink / raw)
  To: info-gnus-english

Hugh Lawson <hlawson@gmail.com> writes:

> To improve my scanty knowledge of elisp, I want to
> rewrite a macro as a function.

Macros are functions that accept code as arguments
(without evaluating it), and then use that code to put
together a new program, which is at last executed.

So macros are programs that write programs (macros are
"metaprograms"). The outcome of the macro invocation
is a function of the dynamically created program, in
turn the result of the code provided as well as the
macro itself.

Compare this to the much simpler model of an ordinary,
static function that just crunches input data and maps
it to the output result!

Macros are hard to understand, read, and write. It is
nothing for beginners, I would say, tho I don't want
to disencourage anyone from doing even difficult
things (unless "difficult" means climbing K2).

One example where macros can be useful is to create
functions that have a syntax that isn't that of the
programming language below (Elisp in this case). After
it is done, those functions can be used transparently
breaking the rules of the syntax. (So macros provide
for "metaprogramming" as well!)

As for me,

   grep defun **/*.el | wc -l

tells me I've 213 functions. I have only one
"defmacro", which I didn't even write. In the same
file, I have a defun which I *did* write, so let me
illustrate:

The function, which isn't the easiest one to read...

    (defun show-time-and-date (&optional insert)
      (interactive "P")
      (let ((date-script "long-date"))
        (if insert (insert-shell-command date-script)
          (with-temp-buffer
            (shell-command date-script 1)
            (message (buffer-substring (point-min) (1- (point-max)) ))))))

...is still one thousand times easier to read than
the macro:

    (defmacro measure-time (&rest body)
      "Measure and return the running time of the code block.
    http://nullprogram.com/blog/2009/05/28/"
      (declare (indent defun))
      (let ((start (make-symbol "start")))
        `(let ((,start (float-time)))
           ,@body
           (- (float-time) ,start))))

> (shell-command COMMAND &optional OUTPUT-BUFFER
> ERROR-BUFFER)

This should be read like this:

1. There is a function called shell-command.

2. shell-command always accepts one argument, which is
   referenced as COMMAND in the documentation and
   command in the code

3. shell-command may also accept two arguments,
   COMMAND and OUTPUT-BUFFER, or three: COMMAND,
   OUTPUT-BUFFER, and ERROR-BUFFER

> 3. I accomplised this with this keyboard macro:
>
> ;;look up latin words (fset 'whitaker [?\M-x ?c ?o ?p
> ?y ?- ?w ?o tab return ?\M-x ?s ?h ?e ?l tab ?- ?c ?o
> ?m ?m tab return ?l ?a ?t ?i ?n ? ?\C-y return])

Oh, no! KEYBOARD macro! Well, I'll just leave the
above... Perhaps it can be education to some.
(Keyboard macros are poor man's programming, plain
automatization of keystrokes, they don't relate to the
defmacro stuff I wrote about.)

If you get it to work with a keyboard macro, then just
use `C-h k' to get the function names. Put them
together in your functions. Check the documentation
for all the functions - sometimes, there are better
ways in Elisp code, than their keyboard/interactive
counterparts. If you are inpatient, you can use this
command

    emacs -batch -f batch-byte-compile source.el

and Emacs will tell you if there are improvements to
be made.

Good luck!

-- 
underground experts united

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

end of thread, other threads:[~2015-01-16 21:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-16 19:29 need help on hobby project Hugh Lawson
2015-01-16 21:01 ` Hugh Lawson
2015-01-16 21:44 ` Emanuel Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).