Recent

Poll

This feature

Cool
13 (50%)
Sucks
13 (50%)

Total Members Voted: 24

Author Topic: Feature idea: array [10] of integer  (Read 16838 times)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9794
  • Debugger - SynEdit - and more
    • wiki
Re: Feature idea: array [10] of integer
« Reply #15 on: January 29, 2015, 05:02:05 pm »
This one would be truly useful. I remember suggesting it at 2009 or 2010 but it was rejected immediately.
I have a related feature request still open for Codetools :
  http://bugs.freepascal.org/view.php?id=15548

I feel absolutely dummy when I have to count the elements. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ..., 55, 56, 57.

Add the following macro (and refine ,or fix (only tested basics). Place caret between at [0..|] (where the total count should and innvoke the macro.

Code: [Select]
var
  porigin, p1, p2: TPoint;
  s: string;
  i, c1,c2, b: integer;
  t: boolean;
  a : char;
begin
  porigin := Caller.CaretXy;

  ecIncrementalFind;
  ecChar('(');
  ecLeft;

  i := 1;
  c1 := 0;
  c2 := 0;
  b := 0;
  t := false;
  repeat
    p1 := Caller.CaretXy;
    ecRight;
    s := Caller.LineAtCaret;
    p2 := Caller.CaretXy;
    if (p1.y = p2.y) and (p1.x = p2.x) then break;

    if (p2.x > length(s)) then begin
      t := false; // string ends in line
      ecdown;
      p2 := Caller.CaretXy;
      if (p1.y = p2.y) then break;  // last line
      s := Caller.LineAtCaret;
      while s = '' do begin
        p1 := Caller.CaretXy;
        ecdown;
        p2 := Caller.CaretXy;
        if (p1.y = p2.y) then break;  // last line
        s := Caller.LineAtCaret;
      end;
      if s = '' then break;
      Caller.CaretX := 1;
      p2 := Caller.CaretXy;
    end;

    if copy (s, p2.x, 2) = '//' then begin
      Caller.CaretX := length(s)+1;
      continue;
    end;

    a := s[p2.x];
    if copy (s, p2.x, 2) = '(*' then  a := '<';
    if copy (s, p2.x, 2) = '*)' then  a := '>';
    if (t) and (copy (s, p2.x, 2) = '''''') then begin
      a := ' ';
      ecRight;;
    end;

    case a of
      '{': if (c2 = 0) and (not t) then c1 := c1 + 1;
      '}': if (c2 = 0) and (c1 > 0) and (not t) then c1 := c1 - 1;
      '<': if (c1 = 0) and (not t) then c2 := c2 + 1;
      '>': if (c1 = 0) and (c2 > 0) and (not t) then c2 := c2 - 1;
      '''': if (c1 = 0) and (c2 = 0) then t := not t;
      '(', '[': if (c1 = 0) and (c2 = 0) and (not t) then b := b + 1;
      ')', ']': if (c1 = 0) and (c2 = 0) and (not t) then begin
          b := b - 1;
          if b < 0 then break;
        end;
      ',': if (c1 = 0) and (c2 = 0) and (not t) then  i := i + 1;
    end;

  until false;

  Caller.CaretXy := porigin;
  Caller.InsertTextAtCaret(inttostr(i), scamAdjust )


end.


Edit [2]:

Added dealing with strings.

You may want to limit how far to search if the closing ")" is missing. Or abort when finding a "begin" keyword.

And: It does not deal with comments before the '('. It goes to the firsn "(" that it finds, comment or not.
« Last Edit: January 29, 2015, 05:29:32 pm by Martin_fr »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Feature idea: array [10] of integer
« Reply #16 on: January 29, 2015, 05:30:42 pm »
Uhh... How can const array be dynamic. "Const" by definition is the opposite of dynamic.
Uhh.. i didn't see that const at the beginning. Typo fixed:
Code: [Select]
// Static
const arr: array[] of integer = (1, 2, 3, 4, 5);
var arr: array[] of integer = (1, 2, 3, 4, 5);

// Dynamic (can change size later)
var arr: array of integer = (1, 2, 3, 4, 5);
« Last Edit: January 29, 2015, 05:33:08 pm by User137 »

A.S.

  • Jr. Member
  • **
  • Posts: 76
Re: Feature idea: array [10] of integer
« Reply #17 on: January 29, 2015, 07:26:46 pm »
Pascal expects ordinal type of index there but "10" is integer constant. In C it means "the number of elements", not the "type of index".
Not sure that it is a good idea.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Feature idea: array [10] of integer
« Reply #18 on: January 29, 2015, 07:39:30 pm »
This already works:
Code: [Select]
type
  TEnum = (eOne, eTwo, eThree);
  TRange = 0..9;

var
  ArrE: array[TEnum] of SmallInt;
  ArrR: array[TRange] of SmallInt;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Feature idea: array [10] of integer
« Reply #19 on: January 30, 2015, 01:11:59 pm »
Add the following macro (and refine ,or fix (only tested basics). Place caret between at [0..|] (where the total count should and innvoke the macro.
...

I opened View -> Editor Macros ...
I recorded a dummy macro, then Edit, then replaced it with your macro from clipboard.
Defined a shortcut, placed the cursor in right spot and run it.
Was it the right thing to do? There could be one more button "Create from Clipboard".

It works somehow, this is a good example. I must study making complex Editor macros, I have only tested with simple ones so far.
Is there any way to debug the macro? I guess not.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Basile B.

  • Guest
Re: Feature idea: array [10] of integer
« Reply #20 on: January 30, 2015, 02:17:30 pm »
This already works:
Code: [Select]
type
  TEnum = (eOne, eTwo, eThree);
  TRange = 0..9;

var
  ArrE: array[TEnum] of SmallInt;
  ArrR: array[TRange] of SmallInt;

+1 very true,

The "enum-indexed-array" is one of my favorite Pascal feature, I use it all the time.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9794
  • Debugger - SynEdit - and more
    • wiki
Re: Feature idea: array [10] of integer
« Reply #21 on: January 30, 2015, 02:29:45 pm »
I opened View -> Editor Macros ...
I recorded a dummy macro, then Edit, then replaced it with your macro from clipboard.
Defined a shortcut, placed the cursor in right spot and run it.
Was it the right thing to do? There could be one more button "Create from Clipboard".

It works somehow, this is a good example. I must study making complex Editor macros, I have only tested with simple ones so far.
Is there any way to debug the macro? I guess not.

Yes that is the way. And yes "create new (empty)" is needed.

I wrote the macro "quick and dirty". It has a lot of potential to clean up and optimize. Instead of all the ecRight, one could just iterate over the entire line string in a for loop.

moskalenco_a

  • New Member
  • *
  • Posts: 19
Re: Feature idea: array [10] of integer
« Reply #22 on: February 28, 2015, 12:16:45 am »
Maybe make arrays totally like in C?! And types like in C. And all like in C.
Code: [Select]
int a,b;
begin
a:=4;
b:=4;//Pascal with C++ it's something strange
a:=a/b;
end.

Septe

  • Jr. Member
  • **
  • Posts: 68
Re: Feature idea: array [10] of integer
« Reply #23 on: April 24, 2015, 05:44:36 am »
Is the ide capable of counting?  Or adding an extension to the ide?  We could highlight the elements, hit the ide element count button, it counts the item between the commas and give you a count?  I'm still learning Lazarus and FPC so I have no clue what's possible, but, this way we can add a little thing to the ide instead of the compiler.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9794
  • Debugger - SynEdit - and more
    • wiki
Re: Feature idea: array [10] of integer
« Reply #24 on: April 24, 2015, 05:53:39 am »
Is the ide capable of counting?  Or adding an extension to the ide?  We could highlight the elements, hit the ide element count button, it counts the item between the commas and give you a count?  I'm still learning Lazarus and FPC so I have no clue what's possible, but, this way we can add a little thing to the ide instead of the compiler.

Yes Pascal Script macros can be added in the IDE (Lazarus) and do it.
I gave an example earlier, but it needs still a few fixes.

 

TinyPortal © 2005-2018