all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* where-are-we: find out the context when reading (any) source code
@ 2010-08-18 15:31 Haojun Bao
  2010-08-18 15:58 ` Leo
  2010-08-18 16:36 ` where-are-we: find out the context when reading (any) source code Andreas Röhler
  0 siblings, 2 replies; 4+ messages in thread
From: Haojun Bao @ 2010-08-18 15:31 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2097 bytes --]

Hi, all

I have wrote a little lisp and a perl script to tell where I am when I'm
reading source code. The lisp code is short: 

    (defun where-are-we ()
      (interactive)
      (save-excursion
        (end-of-line)
        (shell-command-on-region 
         1 (point) 
         (concat "where-are-we " 
                 (or (buffer-file-name) (buffer-name))
                 (format " %s" tab-width)))))

The perl script (also named where-are-we) is attached. When invoked on
the last line of the above lisp code, the output is like following:

    /home/bhj/windows-config/.emacs:1005:              (format " %s" tab-width)))))
        (defun where-are-we ()
          ...
          (save-excursion
            ...
            (shell-command-on-region 
             ...
             (concat "where-are-we " 
                     ...
    =>               (format " %s" tab-width)))))

Another example from reading Android source code:

    /home/bhj/src/android/frameworks/base/libs/ui/EventHub.cpp:474:                             outFlags[codeIndex] = 1;
        /*
         * Inspect the known devices to determine whether physical keys exist for the given
         * framework-domain key codes.
         */
        bool EventHub::hasKeys(size_t numCodes, int32_t* keyCodes, uint8_t* outFlags) {
            for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
                ...
                for (int n = 0; (n < mFDCount) && (outFlags[codeIndex] == 0); n++) {
                    if (mDevices[n]) {
                        ...
                        if (!err) {
                            ...
                            for (size_t sc = 0; sc < scanCodes.size(); sc++) {
                                if (test_bit(scanCodes[sc], mDevices[n]->keyBitmask)) {
    =>                              outFlags[codeIndex] = 1;

Supposedly this can be useful when reading source code and taking note,
even better if used with remember.el. That way, you can easily write a
scenario analysis about which function called which function and so
on...

Hope you can find it useful.


[-- Attachment #2: where-are-we --]
[-- Type: application/octet-stream, Size: 1974 bytes --]

#!/usr/bin/env perl

# report where we are, in which file, on which line, in what context? (in which class, function/method, etc)

# preferably it is used within Emacs, so...
# we take 2 arg, 1st is the file/buffer name, 2nd is the tab-width buffer-local variable. 
# the 2nd arg help us decide the indentation, and the context.
# then we read standard input, it should be region(0, end-of-line(point)) fed from Emacs buffer.

# output should be like: 

    # /home/bhj/src/android-nv-froyo/frameworks/base/libs/ui/EventHub.cpp:328:     while(1) {
    #    
    #     bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,
    #         int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
    #         int32_t* outValue, nsecs_t* outWhen)
    #     {
    #         ...
    #
    # =>      while(1) {

# how we achieve this? 

use Encode;
use utf8;
use strict;

my $line_num = 0;
my @lines = ();
(my $file_name, my $tab_width) = @ARGV;

open(my $file, "-|", "expand -t $tab_width");

while (<$file>) {
  $line_num++;
  chomp;
  $_ = decode_utf8($_);
  push @lines, ($_);
}

@lines = reverse @lines;

$lines[0] =~ m/^\s*/;
my $spaces = length($&);

my @print_lines = ();

my $last_line = shift @lines;
push @print_lines, ("    =>  " . $last_line);

my $limit = 5;
my $outmost = 0;


my $prev_print_line_num = $line_num;
my $last_line_num = $line_num;

for (@lines) {
  $line_num--;
  if ($_ =~ m/^\s*$/) { # skip empty lines
    if ($limit < 5) {
      last;
    } else {
      next;
    }
  }

  $_ =~ m/^\s*/;
  if ($spaces > length($&) or $limit < 5) {
    if ($prev_print_line_num != $line_num+1) {
      push @print_lines, ("        " . (" " x $spaces) . "...");
    }
    $prev_print_line_num = $line_num;
    push @print_lines, ("        " . $_);
    $spaces = length($&);
  }
  if (length($&) == 0) {
    $limit--;
  }
  if ($limit == 0) {
    last;
  }
}

$,="\n";
print "    $file_name:$last_line_num: $last_line\n";
print reverse @print_lines;

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

end of thread, other threads:[~2010-08-18 16:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-18 15:31 where-are-we: find out the context when reading (any) source code Haojun Bao
2010-08-18 15:58 ` Leo
2010-08-18 16:01   ` where-are-we: find out the context when reading (any) sourcecode Drew Adams
2010-08-18 16:36 ` where-are-we: find out the context when reading (any) source code Andreas Röhler

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.