Language Selection

English French German Italian Portuguese Spanish

Script KATE to Automagically Compile/Execute Programs

Filed under
Software
HowTos


Script KATE to Automagically Compile/Execute Programs

By Gary Frankenbery



I teach high school computer science and programming, and I'm trying to transition my computer lab from MS-Windows only to MS-Windows/Linux dual boot.

For Computer Programming, my students first learn to program with Ruby. I try to keep it simple for these fledgeling programmers, so we have been using the Crimson Text Editor under Microsoft Windows to enter/edit the Ruby source code. The Crimson Text Editor makes it easy to configure a macro to execute whatever Ruby script is being edited: that is, the Crimson macro saves the file, then calls up the Ruby interpreter, and Ruby executes the script. Students accomplish this by simply by pressing the F5 function key while in the Crimson Text Editor.

So, I started wondering if I could configure the KDE Advanced Text Editor (KATE) under Linux to do the same thing with Ruby scripts? Yes, I know that KATE has an embedded terminal where one can activate the terminal window, click in it, and then type, for example,"ruby factorial.rb" and press the enter key, to execute the current Ruby script being edited in KATE. But, I wanted to have the current ruby script in the KATE editor to be both automatically saved, and then executed in a separate Konsole window, all triggered with the simple press of a shortcut key--just like Crimson under MS-Windows.

Could I do this? I've got at least one thing going against me--I'm not a bash shell programmer--I don't really know much shell scripting at all. However, I've got some things working for me--I am a programming teacher so I can program, bash scripting is well documented, and I can be very tenacious. So, I believe that with effort, persistance, and and lot of trial and error, I can figure out how to do this with KATE and Linux. And, so, I did. Then, I wanted to do more.

Since my more advanced computer programming students program use Java (not my personal choice--it's the language mandated by the College Board in the U.S.), I figured out how to configure KATE to automatically save and compile a Java program, and then I finally worked out a script to have KATE call up the Java Run Time Environment to execute the Java program. Obviously, other programming languages (Python comes to mind) can be accommodated as well by customizing these scripts (this, as teachers so often (annoyingly) say, is left as an exercise for the reader Smile).

KATE does permit you to embed scripts, so I'll display some KATE screenshots along the way, and explain how it's done. We'll start by configuring three scripts: Ruby Execute, Java Compile, and Java Execute.Then we'll look at how to configure shortcut keys to activate the scripts under KATE.

  1. With KATE on your screen, select Settings -> Configure Kate... from the
    menu.







  2. In the Configure Dialog box shown below, click External Tools in the left panel, then then click the New... button at the bottom.









  3. The Edit External Tool Dialog comes up (below). Here, we're going to configure KATE for Ruby Program Execution. Type in a description Label for the script (I used Ruby Execute). Then type in the script itself, with all the single apostrophes, double quotes, spaces, and symbols. Because this is tricky, you can copy the text on the next line, and paste it into the script box:


    cd "%directory" && konsole -e sh -c 'ruby "%filename"; printf "%s"
    "Press any key to exit . . . "; read $dummy'







    Since the KATE External Tool scripts like an executable name, just type konsole into the Executable: text box. Next, click the drop down list arrow by the
    Save: button, and select Current Document. Click the OK button. This window will
    close. Click OK again. You should now be back at the KATE main window.


  4. Now, let's configure the Java Compile script. Starting at the KATE main window, do steps 1 and 2 again. This time, edit the External Tool Dialog to look like the screen clip below. Again, the script code needs to be exact. Here it is so you can copy and paste, if necessary:



    cd "%directory" && konsole -e sh -c 'javac "%filename"; printf "%s" "Press any key to exit . . . "; read $dummy'








    Click the OK button, and at the next window, click OK again. You should be back at the KATE main window.

  5. Time to configure the last script to execute the Java program. At the KATE main window, do steps 1 and 2 above one more time. You should edit the External Tool Dialog to look like the clip below. Again, here's the scipt command which you can copy and paste:


    cd "%directory" &&  CF="%filename" konsole -e  sh -c 'java ${CF%.*}; printf  "%s" "Press any key to exit . . . "; read $dummy'








    Finally, we are done with the scripting part. Next we'll setup shortcut keys to launch our scripts.

  6. To get started, at the main KATE window, click the Settings->Configure Shortcuts menu choices as shown.








  7. You will see the Configure Shorcuts dialog box window shown below.



    Click the Java Compile script, click the button next to Custom (at the bottom), and press the key(s) you want to launch this script. You can see that I chose the F12 key to compile the current Java program. Click OK



    For the Java Execute script, I chose Shift-F12.




    For executing a Ruby program, I chose I chose Alt+R.



    So, after you configure the three shortcuts, you're done! You can now edit your programming source code in KATE, and quickly save, compile, and execute your Ruby and Java source code with these scripts simply by
    pressing the shortcut key(s).







Script Kate Article

By default, Debian derived Linux distros execute sh scripts using dash rather than bash. Because dash won't work correctly for the above Kate scripts, just substitute bash in place of sh in the above scripts.

Gary Frankenbery
January 4, 2009

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

More in Tux Machines

Doxyqml 0.4.0

After almost two years, here comes a new version of Doxyqml, the QML filter for Doxygen. This new version adds a new command-line option: --namespace to wrap the generated C++ in a namespace, and makes the parser more robust. Nothing ground-breaking, but some nice changes nevertheless. What's interesting with this project is that I don't use it these days, but it still receives contributions from time to time. This puts me in the unusual position (for me) where most of my contributions to the project are reviewing code, cleaning things, a bit of infrastructure (I just added code coverage checks: 88%, not too bad) and release management. Surprisingly, I like doing this, I am happy to see this little tool remains useful enough that others keep it alive. Read more

Plasma 5.12.5, Applications 18.04.1 and Frameworks 5.46.0 by KDE now available in Chakra

On your next system upgrade you will receive all the latest versions of KDE’s Plasma, Applications and Frameworks, in addition to several other package updates. For more details and the full changelogs on KDE’s software releases, you can read the official announcements: Plasma 5.12.5 Applications 18.04.1 Frameworks 5.46.0 Other noteworthy package updates include wine 3.8, skypeforlinux 8.20.0.9 and pypy 6.0.0. Read more

SMTube review - Your train to Youtube

It's a no brainer. On the desktop, you go online, and you open a tab and you load Youtube, and then you play clips. But then, on mobile devices, you have dedicated applications, which usually offer a somewhat more efficient media experience. So, on the desktop, it's the browser way or the ... SMTube way? SMTube is a cross-platform Youtube player, which allows you to search and play videos from the popular media platform, with some additional search tweaks and filters, and extra download options, all this from the desktop, without having to keep a browser tab open. It's a convenient tool to use, and with the recent rewrite, it actually works, and it works fairly well. I decided to test to see what gives. [...] SMTube looks like a nice tool. It is not strictly necessary or needed, but it does allow you to have Youtube open and playing, even if you're not currently using your browser, i.e. you can use it like any other media player. This is nice, plus you get a clean and intuitive interface, decent search and filter options, and it's easy to change settings and configure additional players. You also have the option to download clips. I don't know where SMTube stands when it comes to Google, Youtube, but ordinary users will surely appreciate the extra flexibility they get with a media player rather than just a browser tab. Of course, you're not signed in, you don't get recommendations, comments or playlists, and such, so I guess there are benefits to going directly to Youtube. But if you're only after what Youtube can play without any socializing, SMTube is an excellent choice. It's had a rough ride, it never quite fully worked for me in my various distro reviews, but this new version is stable, robust and works well. At the very least, it's worth testing. Choo choo. Read more

GNOME Development/Developers

  • Nautilus Ability To Launch Binaries Or Scripts To Be Reverted, Might Be Implemented Differently
    It looks like the decision to remove the ability to run binaries and scripts from Nautilus file manager will be reverted. The change comes after some use cases appeared that the developers agreed they need to support, "especially for enterprise and content creators". One such use case that was mentioned as a reason for reverting this is a small "if then that" script for building HTML and PDF files, which uses Zenity to display a dialog, as well as notifications to display the progress. I find the use case being used as an example a bit weird because that's certainly not something common, like a self-extracting game script for instance.
  • Stickers in Riot
    The matrix.org protocol is flexible so this is a good example of how to add new features to the clients that uses matrix without the need to change the protocol. This is not a core feature because you can send images, but I think this is great and add a simple way to show reactions for the users, so as I was reading I thought that we can add this to Fractal, so I started to read how we can add support for this.
  • Talking at GPN 2018 in Karlsruhe, Germany
    Similar to last year I managed to attend the Gulasch Programmier-Nacht (GPN) in Karlsruhe, Germany. Not only did I attend, I also managed to squeeze in a talk about PrivacyScore. We got the prime time slot on the opening day along with all the other relevant talks, including the Eurovision Song Contest, so we were not overly surprised that the audience had a hard time deciding where to go and eventually decided to attend talks which were not recorded. Our talk was recorded and is available here.