Emacs Way – Copying Text

In Emcas if you want to copy a region of text from one file to another then you can just press {C-space} as beginning of copying and then take your cursor to the point till where you want to copy. Then you press {M-w}, M means meta/Alt key and then you will go to the file you want to paste to and put your cursor at the place and press {C-y} and its done. It may look complicated to people who have used Notepad/Wordpad/MS-Office for many years who can just use mouse to copy-paste. Well, it is same except that using keyboard gets much easier over time, plus it kinds of wires into your nervous system. Using mouse to do something never gets easire over time, it remains same.

Now behind the scene, Emacs uses a function called (append-to-buffer) and if you look at the pseudo-code or algorithm, this is how it looks like:

(let (bind-oldbuf-to-value-of-current-buffer)
   (save-excursion                           ; Keep track of buffer.
     change-buffer
     insert-substring-from-oldbuf-into-buffer)
   change-back-to-original-buffer-when-finished
let-the-local-meaning-of-oldbuf-disappear-when-finished

Compare this with how it works in C:

  1. open file for reading, the file you want to copy from
  2. if there was no error in step 1 then open file for writing, where you want to paste
  3. if there was no error in step 2 then check if mark has lower value than point
  4. use fseek to go to the mark
  5. if there was no error in step 4 then read/copy one character and write/paste it
  6. check if copy/pase stopped because of end of work or becauso some error occured in copying.
  7. check if copy/pase stopped because of end of work or becauso some error occured in pasting.

Here is the code in both languages:

(defun append-to-buffer (buffer start end)

  "Append to specified buffer the text of the region.
     It is inserted into that buffer before its point.

     When calling from a program, give three arguments:
     BUFFER (or buffer name), START and END.
     START and END specify the portion of the current buffer to be copied."

  (interactive
   (list (read-buffer "Append to buffer: " (other-buffer
                        (current-buffer) t))
     (region-beginning) (region-end)))

  (let ((oldbuf (current-buffer)))
    (save-excursion
      (let* ((append-to (get-buffer-create buffer))
         (windows (get-buffer-window-list append-to t t))
         point)
    (set-buffer append-to)
    (setq point (point))
    (barf-if-buffer-read-only)
    (insert-buffer-substring oldbuf start end)
    (dolist (window windows)
      (when (= (window-point window) point)
        (set-window-point window (point))))))))  
int copy_buffer_richard(const char *w, const char *r, int pf, int pt)
{
  int rc = 0;
  FILE *fpi = fopen(r, "rb");
  if(fpi != NULL)
    {
      FILE *fpo = fopen(w, "wb");
      if(fpo != NULL)
    {
      int len = pt - pf;
      if(pt > 0 && pf >= 0 && len > 0)
        {
          /* Everything so far has been housekeeping.
         The core of the code starts here... */

          if(0 == fseek(fpi, len, SEEK_SET))
        {
          int ch;

          while((ch = getc(fpi)) != EOF)
            {
              putc(ch, fpo);
            }

          /* ...and ends here. From now on, it's
             just a load more housekeeping. */

          if(ferror(fpi))
            {
              rc = -5; /* input error */
            }
          else if(ferror(fpo))
            {
              rc = -6; /* output error */
            }
        }
          else {
        rc = -4; /* probably the in file is too short */
          }
        }
      else {
        rc = -3; /* invalid parameters */
      }
      fclose(fpo);
    }
      else {
    rc = -2; /* can't open output file */
      }
      fclose(fpi);
    }
  else {
    rc = -1; /* can't open input file */
  }
  return rc;
}
/* by Richard Heathfield */ 

Comparing both, to me Emacs Lisp code is much more easier to understand than C code. C code may look prettier but that is because of lot of extra whitespace around it where Emacs Lisp code in tightly placed. You should look at the pseudo-code of Emacs Lisp on how easier it make connection between pseudo-code and real code. It reads almost like English while C version is, as usual, strikingly odd, pseudo-code and real code look a lot different, typical of C. You may say that comparison is unfair because C is much faster comparing to Emacs Lisp and one file in Emacs Lisp was already opened and I am comparing a full fledged Lisp enviornment with just one single C program. Yeah, I get that, but then again Emacs Lisp code is real code directly taken from source code of Emacs while C code is just written a stand alone, small and short program. Real C program taken from a real life working software will be a lot creepy. In one glance at pseudo-code and real code, you can guess what Emacs Lisp code is doing and it is easier on head whereas real life C code will require lots of glances and will definitely be far from easier on head.

Emacs Lisp version is much more readable and this is a very important point. Ever heard of the sentence called “developer’s time is more important than the machine time” or “a computer program is written once and read 10,000 times” or “Programs must be written for people to read, and only incidentally for machines to execute (Abelson and Sussman, Preface to the First Edition, SICP). Last quote is from one of the most respected books in computer science. If you think those ideas are quite academic or theoretical then you are completely missing the point. Good ideas are not only hard to grasp at first but it is difficult to notice the practical benefit of those too, especially if you are not having few years experience in programming. No matter how much industry is crying about changing customer requirements, good ideas are timeless. These changing customer requirements are nothing but problems that computer programmers solve everday. If, at your workplace, you work mostly in C and C++, you must have noticed almost every company has moved to C++ while two decades back they used to develop mostly in C. More than 65% of the code in this entire world is still in C, but most of it is legacy-code. There is a shift in the thinking that has happened. The programming world keeps on churning out new languages and almost everyone is moving towards the use of languages like C++, Java, Python, Ruby etc. Why is that ? If you look at the new languages, you will notice they were designed more on the side of how to solve the problems in a better way, how can this new language work as a better and improved tool towards solving the problems in or of this world, and indirectly (and may be unknowingly) these language-creators have no interest solving the problems of the machine itself (space and time complexity) because problems of the machine and problems of this world are two points that lie on opposite ends. You can not brilliantly solve the one without ignoring the other by a good amount. C++ was created to solve the problems of large scale software design and hence OO and generic programming paradigms were added. Rather than how to make it more efficient than C, the notion of how to make it better at solving larger problems was choosen. Ruby, Perl, Python and lot of others were created primarily to solve the problems that are not related to machine’s own problems. World is moving from machine towards abstraction. I call it moving to solving problems of this world, moving towards generlization and abstraction, Paul Graham calls it moving from C model to Lisp Model and he is right. Humans always evolve, no matter how many wars and world wars have been fought where humans swore to kill each other, no matter how much negativity and selfishness is there in this world, humans have always evolved and this shift from solving problems of machine to solving problems of this world is a step in further human evolution. Richard Stallman had already evolved to this level by 1984 (along with many other great progrmmers. Good thinking is timeless). He focused more on solving the problem and created this amazing piece of software called Emacs. Thanks to him again.

You should try this book by Robert J. Chassell, it is kind of addictive. When I get some free time it makes me think whether I should entertain myself with a movie or should I just enjoy reading his book :)

<

p style=”text-align:justify;”>
Originally appeared on arnuld’s blog

The Emacs Way of understanding Humans and Computers

I was using gNewSense frow sometime now and one thing about softwares endorsed or created by FSF is that you get to know some amazing ideas or some incredible ways of solving some problems that you never came across before and you yourself never knew those either. For example, take icecat, it comes default with libreJS add-on installed. Generally we think an OS can control your machine and then you. After using libreJS I see how you can use javascript in a web-browser to control the user, without giving any hint at all. User will use his computer for 10 years and for those 10 years he will not have slightest of the idea that he is being controlled. Then I came across duckduckgo search engine and then ThinkPenguin router with 100% Freedom and then h-node and now gNewSense.

When I used Emacs first time, in year 2006, after few weeks of usage I came across The GNU Project (open Emacs and press “Control-h g”), that one keystroke (C-h g) changed my life. I got hooked onto Linux forever (or GNU/Linux as RMS calls it). Since last few years, I have never used/installed any proprietary OS on my machine, my machine runs only on Linux, yes, no other OS, only and only Linux (something that majority of Software Engineering students and prfossionals in INDIA are always scared to do). Just few months back I came across gNewSense and from there I came across one gNewSense BlogPost, an introductory book on programming in Emacs Lisp written by Robert J. Chassell. For those who don’t know, Emacs is one of the most (if not the most) advanced text editors available. I am sure if you make a list of top 20 softwares ever created with best design ideas then Emacs will be one of them (and Apache web server will be there too along with several softwares starting with letter “g” ;) ). Emacs is written using Emacs Lisp (a dialect of Lisp, an amazing language) while some parts are written in C for portability/efficiency reasons. I am using Emacs all the time for writitg code but I do admit I hardly make effective use of it. I think I use may be 10% of its power. I always wanted to learn more and the book written by Robert seemed like a decent start. I am already writing code from few years now and Robert mentioned that it is “not for experienced programmers” but I am reading it anyway because I always wated to understand Emacs and then this book is so interesing and engaging and I can not seem to put it down. It is as much interesting as The Man Who Sold The Moon . Whenever I will come across some idea that I will like then I will post about it here. So, here is the first design idea I really loved (I assume you are familiar with some dialect of Lisp. If not, then just read 17 pages of first chapter of Robert’s book. That will be more than enough. Do not worry, it will not take much time to read those)

  • You want to evaluate something written in Emacs Lisp ? Just open emacs, put cursor at the end of the variable or function name or the closing parenthesis or whatever you want to evaluate and press “C-x C-e” and you got the answer. That’s it, that is how simple it is in Emacs.
  • File and Buffer are two different entities. File is what permamently stored on your computer’s hard disk whereas a buffer is inside emacs which will go away as soon as you exit emacs. A buffer visits the file or is a pointer to the file, not the actual file. You want to save changes into the file then just save the buffer and changes will be written to the file.
  • This one is most interesting. switch-to-buffer is an Emacs Lisp function that helps you in switching to another buffer. Generally when you look at any editor (Notepad, Notepad++ or gedit etc. for example) , you usually look at the file you are editing. If you switch to another file then you will see only and only this another file and previous file will not be visible. Previous file is open but it is not visible and hidden in the editor). What I mean is you can see only one file in front of you, not two (I am not talking about splitting-frames). Within Emacs code, switch-to-buffer is less used than set-buffer. Why ? … Because computer does not need eyes to see while humans do. When a a computer program needs to work on a buffer/file, it does not need to see it, visibility is not needed. switch-to-buffer was designed for humans and it does two things:
    • It switches to the new buffer .
    • It switches the buffer “displayed” in the window with new one.

You can easily guess now that set-buffer only walks the first step, it switches the program to other buffer while buffer on the screen remains unchanged. Doesn’t this concept feel like one of the rules of the creation of this Universe while still being so simple and straightforward. I salute RMS for creating Emacs and keeping it free

Originally appeared at arnuld’s blog.

Happy Software Freedom Day 2014!

Today is the 11th Software Freedom Day.

[ Happy Software Freedom Day 2014 ]

Happy Software Freedom Day 2014

Today is the right day to exercise your freedoms. Here are few things you can do today:

  • Share a copy of your favorite free/libre program with somebody.
  • Get the source code of your favorite free/libre program and skim through the source files.
  • Go to gnu.org/p and get inspired!

If you have suggestions of what can be done today, share them by commenting.

Newbie to gNewSense

I’m a new gNewSense user, but I am not new to GNU/Linux. In 1995, working in the Operations Center at Georgia State University, I read about the Linux kernel on a usenet newsgroup. At the time I was an operator on MVS and Unisys mainframes, and was interested in moving on to Unix, which seemed like a much more interesting environment. Excited that a free Unix-like OS existed, I spent a few days of lunch breaks downloading fifty HD disks of one of the early distributions. I remember it as SLS, but one of my coworkers, who borrowed the disks to install GNU/Linux on one of his own PCs, insists that it was Slackware. I’m inclined to take his word for it.

I’ve been using GNU/Linux ever since. I’ve tried Debian and its various derivatives, several Red Hat based distributions, and even dabbled in Suse. In general I prefer Debian-like systems, but my goal has always been to use the most consistently free distribution I could manage.

I had an Acer Aspire One netbook loaded with Windows 7 that had been essentially abandoned by my wife, and decided to shop for an appropriate GNU/Linux distribution to put it back in service. I’ve recently changed careers from IT  to journalism, and made a committment to myself to systematically build my tool set based on free software. In the spirit of mobile journalism, this laptop is going to be my primary computer.

Two days ago I downloaded gNewSense 3.1 with GNOME desktop for i386, and followed the instructions for creating a live USB. It was a quick and easy process. I then changed the boot order on the netbook, and booted into gNewSense.

My first concern when putting a distribution on a laptop is that the wireless work. It worked like a charm. My second concern is that the out-of-the-box system not be cluttered with things I don’t use. I always customize my environment, but I don’t want to have to do a complete renovation. I’d rather be spending my time adding software I need, than removing software I don’t need.

Every kind of software I use regularly seemed to work, so I backed up the Windows personal folders, and installed gNewSense onto the hard drive of the netbook. At this point the only additional thing I’ve added is GNU Emacs. A web browser and an extensible editor covers at least 90% of what I do with a computer. The other 10% mostly consists of presentation slides and work with spreadsheets, both of which are covered by OpenOffice.

All in all my first impression of gNewSense is very positive. I’m certain that I’ll run into snags along the way, particularly when I start configuring the system to serve as a station for mobile journalism. But it meets my criteria of free and uncluttered and everything I’ve tested works. I’ll continue to post updates as I use and modify the system with additional free software.

Learn Emacs Lisp!

Robert J. Chassell’s Introduction to Programming in Emacs Lisp is a good book to get started with this programming language (and programming in general).

So, why would you want to learn Emacs Lisp in the first place?

To answer that question Robert says:

Perhaps you want to understand programming; perhaps you want to
extend Emacs; or perhaps you want to become a programmer. This
introduction to Emacs Lisp is designed to get you started: to guide
you in learning the fundamentals of programming, and more
importantly, to show you how you can teach yourself to go further.

And Robert makes it very clear that this book was written for folks who are not programmers:

Firstly, I try to say everything at least three times: first, to introduce it; second, to show it in context; and third, to show it in a different context, or to review it.

Secondly, I hardly ever put all the information about a subject in one place, much less in one paragraph. To my way of thinking, that imposes too heavy a burden on the reader. Instead I try to explain only what you need to know at the time. (Sometimes I include a little extra information so you won’t be surprised later when the additional information is formally introduced.)

This book is intended as an approachable hill, rather than as a daunting mountain.

Now, if you’ve the slightest intent to learn Emacs Lisp, I encourage you to jump into this book and get your hands dirty.

First, if you’re not acquainted with GNU Emacs, install it:

# aptitude install emacs

Open Emacs, and do C-h t (hit h while pressing the control key, release both the keys, then press t) to get a nice introduction to Emacs.

Introduction to Programming in Emacs Lisp is best read from within Emacs as an Info manual. To install the book, just do:

$ wget http://cjarry.org/gnu-linux/gnewsense/parkes/gnu-doc_0.2-1_all.deb 

# dpkg -i gnu-doc_0.2-1_all.deb

In Emacs, go to the Info manual tree (C-h i), you’ll find a link to the book (Emacs Lisp Intro).

If you like reading from paper, you can buy the printed version of the book from GNU Press.

Give yourself a lot of time to read the book. Happy learning!

gNewSense 3.1 Released

gNewSense 3.1 Desktop

gNewSense 3.1 Desktop

I’m pleased to announce the release of gNewSense 3.1. This is a minor update to the current stable version with codename Parkes. These are the most important changes:

  • The correct country-specific package repository (instead of beta.gnewsense.org) is set at installation time.
  • Network-manager is included in the live image by default.
  • The expert installer no longer suggests to install Debian’s non-free repository.

Current users of gNewSense 3.0 don’t have to reinstall. They get all updates automatically. However, they should update their software sources, because the installer of gNewSense 3.0 erroneously set the URL of our beta repository as the software source. That repository will be disabled next week, in order to start development on gNewSense 4. I urge you to update your software sources to the correct ones as soon as possible. You can do this as follows:

  1. Open System -> Administration -> Synaptic Package Manager
  2. Go to Settings -> Repositories
  3. Click on each repository that has “beta.gnewsense.org” in the URI and replace “beta.gnewsense.org” with “xx.archive.gnewsense.org”, where “xx” is the code of your country.

So, Germans would replace

http://beta.gnewsense.org/gnewsense-three/gnewsense/

with

http://de.archive.gnewsense.org/gnewsense-three/gnewsense/

Expert users can of course edit their /etc/apt/sources.list directly.

Author: Sam Geeraerts, gNewSense Project Leader.

Usklo: Public Domain Graphics for Your Game

Screenshot from Usklo's website

If you are a game developer or are just starting, and don’t have graphics for your game or game tests, you can use Usklo.

Usklo is an art set for game developers including a race of little invaders from outer space and all their artifacts.

The latest released version includes one character with nine animations, props and one tile set. Usklo is made using free software only and it’s dedicated to the public domain. Its development will continue using gNewSense GNU/Linux.

GET IT!


CC0