Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[geanylua] geany.scintilla() does not work with SCI_SETLEXER #646

Open
Skif-off opened this issue Nov 18, 2017 · 36 comments
Open

[geanylua] geany.scintilla() does not work with SCI_SETLEXER #646

Skif-off opened this issue Nov 18, 2017 · 36 comments

Comments

@Skif-off
Copy link
Contributor

Skif-off commented Nov 18, 2017

I added small script:

--[[
  Copy selected text to new tab
  2017.11.18
--]]

local s = geany.selection();

if (s == "") then
  geany.message("No text is selected!");
elseif (s == nil) then
  geany.message("There is no open document!");
else
  --SCI_GETLEXER
  local l = geany.scintilla(4002);
  geany.newfile();
  geany.selection(s);
  --SCI_SETLEXER
  geany.scintilla(4001, l);
  geany.status("Lua-script: Copy selected text to new tab.");
end

SCI_GETLEXERworks fine, all values (SCLEX_*) were correctly received, but SCI_SETLEXER is not working.
Why? It's bug or not implemented?

Xubuntu 17.10 x64, Geany 1.31, I also checked current Git versions.

[Edit: example changes glspi_newfile here]

@elextr
Copy link
Member

elextr commented Nov 18, 2017

You should not set the lexer directly, set the Geany filetype instead.

@Skif-off
Copy link
Contributor Author

How can I do this? I found geany.fileinfo() with fields type & desc and nothing more.

@elextr
Copy link
Member

elextr commented Nov 18, 2017

Since the Geanylua documentation seems to be absent on the web I can't really help you, a normal plugin would use document_set_filetype() but I don't know if its available in the restricted Geanylua API.

Geanylua being orphaned and unmaintained I'm not sure if the missing documentation problem will get fixed.

@frlan
Copy link
Member

frlan commented Nov 18, 2017

Unlikely if no one adopt it.

@Skif-off
Copy link
Contributor Author

Skif-off commented Nov 18, 2017

@elextr

a normal plugin would use document_set_filetype() but I don't know if its available in the restricted Geanylua API.

"normal" plugin? :)
I use local geanylua-ref.html. Also I tried to find document_set_filetype() in source files of Geanylua, but without success.

Maybe try something like

--- "geanylua/glspi_doc.c"
+++ "geanylua/glspi_doc.c"
@@ -32,12 +32,15 @@
 static gint glspi_newfile(lua_State* L)
 {
 	const gchar *fn=NULL;
+	const gchar *tf=NULL;
 	if (lua_gettop(L)>0) {
 		if (!lua_isstring(L, 1))	{ return FAIL_STRING_ARG(1); }
 		fn=lua_tostring(L, 1);
 		if ( '\0' == fn[0] ) { fn = NULL; }
+		tf=lua_tostring(L, 2);
+		if ( '\0' == tf[0] ) { tf = NULL; }
 	}
-	document_new_file(fn, NULL, NULL);
+	document_new_file(fn, tf, NULL);
 	return 0;
 }

?
Function glspi_newfile, i.e. minimal effort (?). And use like

local t = geany.fileinfo();
geany.newfile("", t.type);

for untitled document/new tab. (I have not tried it yet, just thought.)

Edit: edit "patch"

@elextr
Copy link
Member

elextr commented Nov 19, 2017

a normal plugin would use document_set_filetype() but I don't know if its available in the restricted Geanylua API.

"normal" plugin? :)

By a "normal" plugin I mean one that uses the standard Geany plugin API, C, C++, Cython, anything else that accesses C and compiles to a .so for eg Rust, and Python via Geanypy that mapped almost all of the Geany API to Python and @kugel-s Peasy that uses GI to make all the Geany API available to Python and some other languages I believe.

But IIRC Geanylua provided only a customised and limited subset of that API. If the functionality is not available then I don't think you will be able to do whatever you are trying to do.

To explain further, there are several reasons to use the Geany filetype, not set the lexer directly:

  1. Geany expects its in control of the lexer, it will set it or reset it at any time it feels the need to, overwriting your selection.

  2. To make highlighting work you need more than the lexer, you need to map the style results to the styles set by the filetype/colour scheme, and thats hard coded in Geany based on what Geany expects the lexer to be from the filetype, not whats set in Scintilla.

@elextr
Copy link
Member

elextr commented Nov 19, 2017

But IIRC Geanylua provided only a customised and limited subset of that API. If the functionality is not available then I don't think you will be able to do whatever you are trying to do.

Won't be able to do it in Geanylua that is.

@Skif-off
Copy link
Contributor Author

I tried to use glspi_newfile mod, but Geany crash with error

(geany:3157): Geany-CRITICAL **: filetypes_load_config: assertion 'ft_id < filetypes_array->len' failed

Need to think.

@elextr
Copy link
Member

elextr commented Nov 19, 2017

Ok, you can do it if you extend your own Geanylua. And since its unowned, why not :)

You C code is passing a string as the filetype, it should be a pointer to the filetype structure, use filetypes_lookup_by_name() to get it from the string.

[Edit: s/cod/code/ its not THAT fishy :) ]

@Skif-off
Copy link
Contributor Author

Skif-off commented Nov 19, 2017

Hmm, seems to work fine and I think I fixed two warnings... Make PR?

@elextr
Copy link
Member

elextr commented Nov 19, 2017

@frlan do you accept PRs for orphaned plugins?

@frlan
Copy link
Member

frlan commented Nov 19, 2017

@elextr @Skif-off Why not? But not before 1.32 and I prefer PR with an updated MAINTAINERS file too ;)

@Skif-off
Copy link
Contributor Author

Skif-off commented Nov 19, 2017

@frlan

But ... I prefer PR with an updated MAINTAINERS file too ;)

Sorry, it's too cool for me :) And I found a workaround:

  local t = geany.fileinfo();
  geany.newfile("UNTITLED" .. t.ext);

(but untitled document would be more convenient because it's obvious).

Well, I do not want to lose, maybe will useful :), patch (like document.new_file in GeanyPy, + upd geanylua-ref.html):

diff --git a/geanylua/glspi_doc.c b/geanylua/glspi_doc.c
index 888ce8d5..b536033e 100644
--- a/geanylua/glspi_doc.c
+++ b/geanylua/glspi_doc.c
@@ -32,12 +32,23 @@ static gint glspi_filename(lua_State* L)
 static gint glspi_newfile(lua_State* L)
 {
        const gchar *fn=NULL;
-       if (lua_gettop(L)>0) {
+       GeanyFiletype *ft=NULL;
+       switch (lua_gettop(L)) {
+       case 0: break;
+       case 2:
+               if (!lua_isstring(L, 2))        { return FAIL_STRING_ARG(2); }
+               const gchar *tmp=lua_tostring(L, 2);
+               if ( '\0' == tmp[0] ) {
+                       ft=NULL;
+               } else {
+                       ft=filetypes_lookup_by_name(tmp);
+               }
+       default:
                if (!lua_isstring(L, 1))        { return FAIL_STRING_ARG(1); }
                fn=lua_tostring(L, 1);
-               if ( '\0' == fn[0] ) { fn = NULL; }
+               if ( '\0' == fn[0] ) { fn=NULL; }
        }
-       document_new_file(fn, NULL, NULL);
+       document_new_file(fn, ft, NULL);
        return 0;
 }
 

diff --git a/geanylua/docs/geanylua-ref.html b/geanylua/docs/geanylua-ref.html
index 9795c165..0c92b246 100644
--- a/geanylua/docs/geanylua-ref.html
+++ b/geanylua/docs/geanylua-ref.html
@@ -136,7 +136,7 @@ The Geany module provides these functions and variables...<br><br><br>
 </tr>
 
 <tr class="odd">
-  <td>&nbsp; function <a href="#newfile"><b>newfile</b></a> ( [filename] )<br></td>
+  <td>&nbsp; function <a href="#newfile"><b>newfile</b></a> ( [filename [, filetype] )<br></td>
   <td class="desc">-- Create a new document.</td>
 </tr>
 
@@ -756,10 +756,23 @@ the selection is treated as <i>rectangular</i> rather than <i>linear</i>.<br>
 <br><br>
 
 
-<a name="newfile"></a><hr><h3><tt>geany.newfile ( [filename] )</tt></h3><p>
-When called with one argument, creates a new document with the specified
-<tt>filename</tt>. </p><p>When called with no arguments, creates a new, untitled document.
-</p><br><br>
+<a name="newfile"></a><hr><h3><tt>geany.newfile ( [filename [, filetype] )</tt></h3><p>
+<p>When called with no arguments, creates a new, untitled document.</p>
+<p>When called with one argument, creates a new document with the specified
+<tt>filename</tt>.</p>
+<p>When called with two argument, creates a new document with the specified
+<tt>filename</tt> and <tt>filetype</tt> (a one-word description of the filetype,
+e.g. "C" or "Python".). If you want untitled document then set <tt>filename</tt> as <tt>""</tt>.</p>
+<p>So you can use it like this:</p>
+<pre>local s = geany.selection();
+
+if (s ~= "") and (s ~= nil) then
+  local t = geany.fileinfo();
+  geany.newfile("", t.type);
+  geany.selection(s);
+end</pre>
+<p>(create a new, untitled document, with selected text and auto set filetype).</p>
+<br><br>
 
 
 

Edit: del all warnings fixes, add freeing memory, fix indents
Edit2: upd patch (upd geany.newfile description)
Edit3: upd patch (upd geany.newfile)

@Skif-off
Copy link
Contributor Author

Hmm... I'm looking at Nightly Builds stdout and I have question: how to fix

glspi_app.c: In function ‘glspi_launch’:
glspi_app.c:425:6: warning: argument 1 range [18446744056529682433, 18446744073709551609] exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
  argv=g_malloc0(sizeof(gchar *)*argc+1);

? And why Nightly Builds without this warning?

Xubuntu 17.10

@elextr
Copy link
Member

elextr commented Nov 21, 2017

You have items of mixed signedness in the expression. This is IIRC a fairly new stupid warning. To shut it up just cast argc to an unsigned type. The nightly probably uses a compiler thats older than the stupid warning.

@Skif-off
Copy link
Contributor Author

@elextr
gchar **argv=NULL; >>> guchar **argv=NULL;?
Is everything will be fine if I just ignore this warning?

@elextr
Copy link
Member

elextr commented Nov 21, 2017

No its not the gchar* thats the problem, its that argc is int which is signed, but sizeof returns size_t which is unsigned.

Thats why its a stupid warning (or at least stupid to enable by default) because many system interfaces use int as a counter (like argc) and everybody knows they are never negative ... except the compiler, so it makes this stupid error.

just (guint)(argc) will probably work.

@Skif-off
Copy link
Contributor Author

Can I just ignore this warning? :)

@elextr
Copy link
Member

elextr commented Nov 21, 2017

For your own stuff sure.

@codebrainz
Copy link
Member

Thats why its a stupid warning (or at least stupid to enable by default) because many system interfaces use int as a counter (like argc) and everybody knows they are never negative

To be fair, it's not the actual argc, it's a random value popped off the Lua stack, it could be anything and the code only guards against value 0, not < 0.

Also, unrelated, that line looks wrong because of the order of operations and missing parenthesis, I believe it will allocate room for the needed pointers plus one byte. Presumably it's meant to be sizeof(gchar*) * (argc+1) to allocate storage for the number of pointers plus one extra pointer for the sentinel NULL as is customary with argv/GStrv.

To fix the warning and bug, it could probably be changed to:

argv = g_malloc0_n(argc+1, sizeof(gchar*));

@elextr
Copy link
Member

elextr commented Nov 21, 2017

I believe it will allocate room for the needed pointers plus one byte.

@codebrainz good catch.

Luckily for any geanylua users the allocated memory is likely to round up to a multiple of a pointer anyway for alignment reasons.

@Skif-off
Copy link
Contributor Author

Skif-off commented Nov 23, 2017

I tried

argv = g_malloc0_n(argc+1, sizeof(gchar*));

and I have:

glspi_app.c: In function ‘glspi_launch’:
glspi_app.c:425:7: warning: argument 1 range [18446744071562067969, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
  argv = g_malloc0_n(argc+1, sizeof(gchar*));
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/glib-2.0/glib/glist.h:32:0,
                 from /usr/include/glib-2.0/glib/ghash.h:33,
                 from /usr/include/glib-2.0/glib.h:50,
                 from /usr/local/include/geany/tagmanager/tm_source_file.h:14,
                 from /usr/local/include/geany/tagmanager/tm_tag.h:32,
                 from /usr/local/include/geany/app.h:31,
                 from /usr/local/include/geany/geanyplugin.h:36,
                 from glspi.h:19,
                 from glspi_app.c:15:
/usr/include/glib-2.0/glib/gmem.h:96:10: note: in a call to allocation function ‘g_malloc0_n’ declared here
 gpointer g_malloc0_n      (gsize  n_blocks,
          ^~~~~~~~~~~
  CC       libgeanylua_la-glspi_dlg.lo

Xubuntu 17.10 x64,

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.2.0-8ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3) 

@Skif-off
Copy link
Contributor Author

Maybe I'd better use gcc-6 (6.4.0) or gcc-5 (5.5.0)?

@elextr
Copy link
Member

elextr commented Nov 23, 2017

or set option -Wno-alloc-size-larger-than

@codebrainz
Copy link
Member

@Skif-off try this:

argv=g_malloc0_n(argc + 1u, sizeof(gchar *));

@codebrainz
Copy link
Member

This also works:

diff --git a/geanylua/glspi_app.c b/geanylua/glspi_app.c
index ed82d5eb..23818170 100644
--- a/geanylua/glspi_app.c
+++ b/geanylua/glspi_app.c
@@ -418,11 +418,11 @@ static gint glspi_launch(lua_State* L)
 	gchar **argv=NULL;
 	gboolean rv;
 	GError *err=NULL;
-	if (argc==0) { return FAIL_STRING_ARG(1); }
+	if (argc<=0) { return FAIL_STRING_ARG(1); }
 	for (i=1;i<=argc;i++) {
 		if (!lua_isstring(L,i)) { return FAIL_STRING_ARG(i); }
 	}
-	argv=g_malloc0(sizeof(gchar *)*argc+1);
+	argv=g_malloc0_n(argc+1, sizeof(gchar *));
 	for (i=0;i<argc;i++) {
 		argv[i]=(g_strdup(lua_tostring(L,i+1)));
 	}

@Skif-off
Copy link
Contributor Author

@codebrainz
Yes, both variant works fine. Only one warning:

In file included from /usr/include/x86_64-linux-gnu/sys/stat.h:25:0,
                 from glspi_app.c:8:
/usr/include/features.h:183:3: warning: #warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE" [-Wcpp]
 # warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE"
   ^~~~~~~

I added line #define _DEFAULT_SOURCE /* fix warning with glibc >= 2.20 */ below.

@codebrainz
Copy link
Member

@Skif-off yeah, I saw that warning, but it's just a deprecation warning.

Proper fix would be for GeanyLua to use build system to check for stat()/lstat() and let autotools handle defining such macros as needed for the proper versions, IMO.

@codebrainz
Copy link
Member

Something like this:

diff --git a/configure.ac b/configure.ac
index 7fb7d402..fa3bf5a2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -9,6 +9,7 @@ AC_CONFIG_SRCDIR([po/POTFILES.in])
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_MACRO_DIR([build/cache])
 
+AC_USE_SYSTEM_EXTENSIONS
 AC_PROG_CC
 AC_PROG_CC_C99
 AM_PROG_CC_C_O
diff --git a/geanylua/glspi_app.c b/geanylua/glspi_app.c
index 23818170..f6379f74 100644
--- a/geanylua/glspi_app.c
+++ b/geanylua/glspi_app.c
@@ -4,7 +4,10 @@
  * See the file "geanylua.c" for copyright information.
  */
 
-#define _BSD_SOURCE /* for stat() and lstat() */
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <unistd.h>

@Skif-off
Copy link
Contributor Author

@codebrainz maybe you'll make a PR? As part of the development team :)

@Skif-off
Copy link
Contributor Author

  1. If I want to add forced_enc, then where I can to find list of them and how to use? GEANY_ENCODING_* from encodings.h or strings from encodings.c? (I didn't find examples.)

  2. Where I can find files for build deb-files from master-branch (or for nightly builds)? Or it's not public? I didn't find :) (Nofiles geany_*.debian.tar.xz & geany-plugins_*.debian.tar.xz for 1.32 and I couldn't do it myself for Utils and Workbench.)


P.S. geany.reloadconf (like geany.reload_configuration() from GeanyPy) may be of interest...

diff --git a/geanylua/glspi_app.c b/geanylua/glspi_app.c
index ed82d5eb..b59d6b9a 100644
--- a/geanylua/glspi_app.c
+++ b/geanylua/glspi_app.c
@@ -604,6 +604,12 @@ static gint glspi_keygrab(lua_State* L)
 }
 
 
+static gint glspi_reloadconf(lua_State* L)
+{
+	main_reload_configuration();
+	return 0;
+}
+
 
 static const struct luaL_reg glspi_app_funcs[] = {
 	{"pluginver", glspi_pluginver},
@@ -620,6 +626,7 @@ static const struct luaL_reg glspi_app_funcs[] = {
 	{"keycmd",    glspi_keycmd},
 	{"launch",    glspi_launch},
 	{"keygrab",   glspi_keygrab},
+	{"reloadconf", glspi_reloadconf},
 	{NULL,NULL}
 };
 

@elextr
Copy link
Member

elextr commented Nov 28, 2017

If I want to add forced_enc, then where I can to find list of them and how to use? GEANY_ENCODING_* from encodings.h or strings from encodings.c? (I didn't find examples.)

Its any name known to your system iconv, so its system specific, Geany has some common names in its menus, but in fact even those are not guaranteed to be available.

Where I can find files for build deb-files from master-branch

The project does not package Geany for any distros, its done by external packagers who are experts in the peculiarities of their distro. So the project has no package building scripts.

@codebrainz
Copy link
Member

P.S. geany.reloadconf (like geany.reload_configuration() from GeanyPy) may be of interest...

You should make separate pull requests for the above patches if you want to get them merged. That plugin doesn't have a dedicated maintainer, so making it easy on committers will give you a better chance to get changes in ... or if you volunteer to become the new maintainer :)

@Skif-off
Copy link
Contributor Author

@elextr

Its any name known to your system iconv, so its system specific, Geany has some common names in its menus, but in fact even those are not guaranteed to be available.

Thanks, here it is necessary to think.

The project does not package Geany for any distros, its done by external packagers who are experts in the peculiarities of their distro. So the project has no package building scripts.

I saw https://nightly.geany.org/ and thought that it's a part of the development team :) Ok, I'll wait for the packages 1.32 in the repository.

@codebrainz

You should make separate pull requests for the above patches if you want to get them merged.

Ok, I'll do it today-tomorrow.

or if you volunteer to become the new maintainer :)

It's impossible :) I like Geany, I like GeanyLua (with lua-utf8 :)), but I am not programmer: I know some script languages (AutoIt, Lua, JScript/VBScript) a little, I can understand a little bit С/FreePascal and I can do something (with help/examples), but maintainer... Sorry :(

@elextr
Copy link
Member

elextr commented Nov 29, 2017

Its any name known to your system iconv, so its system specific, Geany has some common names in its menus, but in fact even those are not guaranteed to be available.

Thanks, here it is necessary to think.

As far as we know there is no programmatic way of getting a list of the available encodings at runtime, (the equivalent of the iconv --list command). If you find one let us know.

I saw https://nightly.geany.org/ and thought that it's a part of the development team :)

Yes the nightlys are provided by one of the developers, but they are for checking the build on a number of configurations, the packages produced are intended only as an artifact, although they may work if your system matches the build one. But the scripts are not published AFAIK and probably the build scripts are very specific to the particular setup and would not be useful even if they were published.

Packages for Debian are not built by the packagers any more IIUC, the packagers submit jobs to a Debian build farm that makes packages for all the platforms and systems Debian supports, so only authorised people can make packages for Debian. Ubuntu is the same IIUC. Other distros may vary :) Packagers are of course individual contributors and their ability to immediately respond to a new Geany release may vary from time to time.

@Skif-off
Copy link
Contributor Author

@elextr

Its any name known to your system iconv, so its system specific, Geany has some common names in its menus, but in fact even those are not guaranteed to be available.

Thanks, here it is necessary to think.

As far as we know there is no programmatic way of getting a list of the available encodings at runtime, (the equivalent of the iconv --list command). If you find one let us know.

Well, now I understand why I did not find examples of using this feature :))

and probably the build scripts are very specific to the particular setup and would not be useful even if they were published.

I don't think so and they are not much different from Debian repositories (geany, geany-plugins) :) I use Debian-based OS and I wanted to see geany-plugins-common.install (libgeanypluginutils.so*) only (I think that geany-plugin-workbench.install & geany-plugin-keyrecord.install like all the others).
But now I think I understood how to do it, I will check it.
(I'm make deb files for myself because I'm using PR #1017 for Geany.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants