! Please note that this is a snapshot of our old Bugzilla server, which is read only since May 29, 2020. Please go to gitlab.xfce.org for our new server !
Infer filetype from shebang line
Status:
RESOLVED: FIXED
Severity:
enhancement
Product:
Mousepad
Component:
General

Comments

Description carlosjosepita 2013-11-26 04:31:26 CET
Now that there is syntax highlighting and color themes support, a little more sofisticated type inference  would be pretty useful and easy to implement. A lot of scripts haven't any extension at all. Maybe the file command could be used. It would be a oneliner and it's not a heavy dependency.
Comment 2 Andre Miranda editbugs 2014-11-20 04:09:14 CET
Matt, gEdit(3.14.1) is inferring the filetype from shebang. I thought that compiling Mousepad with GTK3 support(thus GtkSourceView3) would "fix" this.

I might be wrong, but the gEdit source[1] related to guessing filetype isn't that different from Mousepad[2], so what is preventing Mousepad from guessing just like gEdit?

1 - https://git.gnome.org/browse/gedit/tree/gedit/gedit-document.c#n595
2 - http://git.xfce.org/apps/mousepad/tree/mousepad/mousepad-file.c#n450
Comment 3 tuxd3v 2018-10-16 07:27:39 CEST
Hello Guys,
I started using mousepad soma days ago, and I like it :)
Simple fast low resources..

It has a draw back, which is on this Bug Report.
I am using mousepad v0.4.0

I tried to clone repo, and build, it, first to test first master branch..and see if I could solve this bug, but unfortunately I am on aarch64, and have some conflicts trying to install libgtk2.0-dev, because of my mitgard graphic card..
package manager got corrupted and was a lot of trouble to revert...it conflict with some X11 packages..

It doesn't detect files with shebang.
I need to assign file type to have syntax highlighting each time I save the file, its like crazy.
Does this Bug had already been solved?


Thanks in Advance. 
tux
Comment 4 Matthew Brush editbugs 2018-10-17 03:32:19 CEST
@tuxd3v you could use `./configure --enable-gtk3` to build against GTK+ 3, but I don't think it will work still.

I guess if it's not done by GtkSourceView or GContentType, it would have to be done in Mousepad code. It seems easy to actually find and parse the shebang, but there would need to be a mechanism to match up the arbitrary shell commands to a language type, which seems like it would be more appropriate in GtkSourceView since it provides those or GContentType since it knows about mime/file types.

Maybe the correct solution would be to add a `<shebangs>` element to the GSV XML language[0] files and related code to the library? Or maybe even a more generic regex rather like `<globs>` that looks in the first or last N bytes of the file to detect also emacs/vim style special comments telling the language in addition to shebangs? It's possible this already exists and I just didn't see it in the docs.

[0]: https://developer.gnome.org/gtksourceview/stable/lang-reference.html
Comment 5 Tamaranch 2019-11-20 18:31:23 CET
Hi,

Is there any news on this?
shared-mime-info and gtksourceview are properly configured on my system: a file starting by "#!/bin/bash" is recognized as a shell script, regardless to its extension, opened by mousepad, but not automatically highlighted if its extension is different from ".sh".
Is there a way to make automatic highlighting being based on files content?
Thanks!
Comment 6 jmcamp 2020-03-11 22:04:17 CET
I created a patch based on gedit:  https://github.com/jaseemabid/gedit/blob/master/gedit/gedit-document.c#L1032

I've been using mousepad with the patch for a couple weeks and it's been working OK for me so far.


--- a/mousepad/mousepad-file.c
+++ b/mousepad/mousepad-file.c
@@ -462,15 +462,24 @@
   gchar             *basename;
   gboolean           result_uncertain;
   GtkSourceLanguage *language;
+  gchar             *data;
+  GtkTextIter        start;
+  GtkTextIter        end;
 
   g_return_val_if_fail ((file->filename != NULL), NULL);
 
-  content_type = g_content_type_guess (file->filename, NULL, 0, &result_uncertain);
+  gtk_text_buffer_get_start_iter (file->buffer, &start);
+  end = start;
+  gtk_text_iter_forward_chars (&end, 255);
+  data = gtk_text_buffer_get_text (file->buffer, &start, &end, TRUE);
+
+  content_type = g_content_type_guess (file->filename, (const guchar *)data, strlen (data), &result_uncertain);
   basename = g_path_get_basename (file->filename);
   language = gtk_source_language_manager_guess_language (gtk_source_language_manager_get_default (),
                                                          basename,
                                                          result_uncertain ? NULL : content_type);
 
+  g_free (data);
   g_free (basename);
   g_free (content_type);
Comment 7 Tamaranch 2020-03-12 20:07:46 CET
This patch worked for me on Arch Linux.
This is very nice to have shell scripts (among others) recognized regardless to files extensions!
Thanks @jmcamp, I hope this patch will be merged soon!
Comment 8 Andre Miranda editbugs 2020-03-24 00:03:59 CET
The patch looks good to me, can you please attach it commit info (git format-patch)? Thanks.
Comment 9 jmcamp 2020-03-25 03:16:31 CET
(In reply to Andre Miranda from comment #8)
> The patch looks good to me, can you please attach it commit info (git
> format-patch)? Thanks.


Sorry I don't know git and don't have any free time now to look into it.  The last time I used a version control system was SVN about 10 years ago!
Comment 10 Andre Miranda editbugs 2020-03-25 14:38:36 CET
(In reply to jmcamp from comment #9)
> Sorry I don't know git and don't have any free time now to look into it. 
> The last time I used a version control system was SVN about 10 years ago!

Can I make you the author of the commit (name+email) or you don't mind about authorship?
Comment 11 jmcamp 2020-03-25 15:51:34 CET
> (In reply to jmcamp from comment #9)
> > Sorry I don't know git and don't have any free time now to look into it. 
> > The last time I used a version control system was SVN about 10 years ago!
> 
> Can I make you the author of the commit (name+email) or you don't mind about
> authorship?

I don't mind about authorship, so feel free to commit however you see fit.  (In reply to Andre Miranda from comment #10)
Comment 12 Git Bot editbugs 2020-03-26 00:24:12 CET
Andre Miranda referenced this bugreport in commit f41614cc3ecd1032de632d21a89024ccafae209b

Infer filetype from shebang line (Bug #10514)

https://git.xfce.org/apps/mousepad/commit?id=f41614cc3ecd1032de632d21a89024ccafae209b

Bug #10514

Reported by:
carlosjosepita
Reported on: 2013-11-26
Last modified on: 2020-03-26

People

Assignee:
Matthew Brush
CC List:
8 users

Version

Version:
Unspecified
Target Milestone:
Mousepad 0.2.x

Attachments

Additional information