all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Defining  functions on the fly
Date: Tue, 16 Jun 2015 13:35:53 +0200	[thread overview]
Message-ID: <87zj3zubqe.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.5087.1434450412.904.help-gnu-emacs@gnu.org

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> Am 16.06.2015 um 11:50 schrieb Pascal J. Bourguignon:
>> There are hooks to customize those general commands to specific modes!
>> For example, instead of writing:
>>
>>      bash-forward-sexp
>>      sh-forward-sexp
>>      pascal-forward-sexp
>>      ada-forward-sexp
>>
>> etc, each mode will just bind a specific forward sexp function to the
>> hook variable:  forward-sexp-function, and forward-sexp will call it.
>>
>>
>
> That's a consideration at the command- resp. key-binding level.
> Nonetheless mode-specific functions are needed to bind then.

Of course, and mode-specific functions will have a mode-specific prefix
in their name.

But if you have functions that are not mode-specific, like,
    (generate-parser mode-specific-grammar), or
    (parse-to-point mode-specific-grammar), 
you DO NOT DUPLICATE those functions with names such as
    pascal-generate-parser  pascal-parse-to-point
    ada-generate-parser     ada-parse-to-point
    c-generate-parser       c-parse-to-point
    bash-generate-parser    bash-parse-to-point
This is dumb.

What you do is to call those general functions from your mode-specific
functions (that since they are specific and different for each mode, do
not contain duplicated code).

What you could do, for those generic functions, is to put them in a nice
re-usable library, give a name to the library, and prefix them by the
name of the library. Eg. if you name the library bovidae, then you will
have:

     bovidae-generate-parser
     bovidae-parse-to-point

but you would not duplicate those functions with names such as:
    pascal-bovidae-generate-parser  pascal-bovidae-parse-to-point
    ada-bovidae-generate-parser     ada-bovidae-parse-to-point
    c-bovidae-generate-parser       c-bovidae-parse-to-point
    bash-bovidae-generate-parser    bash-bovidae-parse-to-point
This would be dumb.


> For now these language mode often invent very basic things in
> parallel: string-strip, in-string-p etc.
> Also creating bugs and quirks that way.

Then factorize that out in a common library!


> Beyond that we can generalize top-level, block, expression and
> statement -- at least to some extend.

You already have a few such functions:

    beginning-of-defun
    end-of-defun
    indent-defun
    mark-defun
    narrow-to-defun


Also, I've noticed a remark in the thread.

Some languages distinguish statements and expressions (and sometimes
also declarations or definitions), but some other languages only have
expressions.  The laters are much more usable and agreable, and more
easily edited also.

When parsing a program, you naturally obtain parse trees labelled with
the kind statement, expression, definition, etc.  But when editing, you
might want to flatten all this down to expressions (sexp), as if you
were editing lisp, because it is easier.  But you could provide tools to
convert between statements and expressions.

For example, if you kill a C statement (selection shown with []):

   [ x=a+b; ]
     if(a==b){
       x=0;
     }else{
       y=42;
     }

then move into an expression context (point shown with |):

     if(|a==b){
       x=0;
     }else{
       y=42;
     }

and yank, the statement x=a+b; could be transformed into an expression
(semicolon removed):

     if((x=a+b),|(a==b)){
       x=0;
     }else{
       y=42;
     }

And backward, an expression:

   x=[ (a==b)?0:42 ];

killed and yanked into statement context:

   {
    |
   }

would transform it into a statment:

   {
    if(a==b){
     return 0;
    }else{
     return 42;
    }
  }   

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


  parent reply	other threads:[~2015-06-16 11:35 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-15  9:24 Defining functions on the fly Andreas Röhler
2015-06-15  9:32 ` Dmitry Gutov
2015-06-15  9:47 ` Alexis
2015-06-15 10:20   ` Andreas Röhler
2015-06-15 10:37     ` Alexis
2015-06-15 10:41     ` Michael Heerdegen
2015-06-15 10:42     ` Tassilo Horn
2015-06-15 11:01       ` Alexis
2015-06-15 11:19       ` Michael Heerdegen
2015-06-15 11:24         ` Tassilo Horn
2015-06-15 11:31           ` Michael Heerdegen
2015-06-15 10:00 ` Tassilo Horn
2015-06-15 10:33   ` Andreas Röhler
     [not found] ` <mailman.5022.1434361680.904.help-gnu-emacs@gnu.org>
2015-06-15 12:52   ` Stefan Monnier
2015-06-16  5:59     ` Andreas Röhler
2015-06-16  7:01       ` Tassilo Horn
2015-06-16  9:22         ` Andreas Röhler
2015-06-16 11:12           ` Tassilo Horn
2015-06-16 11:40             ` Andreas Röhler
2015-06-16 13:10               ` Tassilo Horn
2015-06-16 15:46                 ` Andreas Röhler
     [not found]                 ` <mailman.5109.1434469628.904.help-gnu-emacs@gnu.org>
2015-06-16 22:34                   ` Stefan Monnier
     [not found]             ` <mailman.5090.1434454815.904.help-gnu-emacs@gnu.org>
2015-06-16 16:13               ` Pascal J. Bourguignon
2015-06-16 18:30                 ` Andreas Röhler
2015-06-16  9:42         ` Andreas Röhler
     [not found]         ` <mailman.5084.1434446560.904.help-gnu-emacs@gnu.org>
2015-06-16  9:50           ` Pascal J. Bourguignon
2015-06-16 10:26             ` Andreas Röhler
     [not found]             ` <mailman.5087.1434450412.904.help-gnu-emacs@gnu.org>
2015-06-16 11:35               ` Pascal J. Bourguignon [this message]
     [not found]         ` <mailman.5085.1434447782.904.help-gnu-emacs@gnu.org>
2015-06-16 22:32           ` Stefan Monnier
     [not found]     ` <mailman.5075.1434434385.904.help-gnu-emacs@gnu.org>
2015-06-16 22:23       ` Stefan Monnier
2015-06-17  6:13         ` Andreas Röhler
     [not found]         ` <mailman.5137.1434522533.904.help-gnu-emacs@gnu.org>
2015-06-17 14:30           ` Stefan Monnier
2015-06-17 16:40             ` Andreas Röhler
     [not found] <mailman.5020.1434360277.904.help-gnu-emacs@gnu.org>
2015-06-15  9:57 ` Pascal J. Bourguignon
2015-06-15 11:21   ` Andreas Röhler
2015-06-15 11:52     ` Tassilo Horn
2015-06-15 12:06       ` Andreas Röhler
2015-06-15 12:43       ` Andreas Röhler
2015-06-15 23:51 ` Gene
2015-06-16  1:14   ` Stefan Monnier
2015-06-17  0:24     ` Gene
2015-06-17  1:12       ` Stefan Monnier
2015-06-16  5:47   ` Andreas Röhler

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=87zj3zubqe.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.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.