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

GNOME News

  • The Begining
    The friendship/relationship with the awesome community of GNOME begins. What followed after 2 commits into the main branch, one application submission, and the result was the start of the most amazing few months. These months have been a humbling experience, the biggest learning experience, and the most productive time.
  • GTK+ Tester Window?
    For an internal application, I’ve created a Gtk.Window derived tester class, added some widgets to show current test, status, number of fails and a Gtk.Grid to attach custom widgets. This class expose some API to set a widget to test, autoclose and some signals you can use to run some tests.
  • GUADEC 2016
    A lot of great things happened – as always GUADEC with it’s perfect size got me to speak to a hell lot of new and interesting people. Thank you all for being there – it was a pleasure.

Leftovers: OSS and Sharing

  • #MyOpenHA Part 1 -Philosophy
    Home Automation. The holy hipster and geek grail. I have played with it. I have tried. I have failed. But today I am proud to have a solution I can truly endorse. So join me on this journey. This series will explain my solution, in excruciating detail. In the hope that I can learn from you while I am explaining. This series will be filled over time with more and more articles. But now, let’s talk about philosophy. The Why. Soon you will see the What and How. One promise, or the TL;DR: It is all 100% Open Source. Well, almost. I have integrated some quite non-open things but always in an Open Source Way.
  • Disable the new Firefox 48 location bar - Tutorial
    Here we are. Seven minutes later, our life is bearable again, but not perfect. Thank you Mozilla, thank you very much. This is exactly what I needed to enrich my life. After all, we all know, cosmetic changes are good, because that's what plants crave. Stop with these idiotic tweaks please. No one cares. It won't make the browser better. It won't change the market share. It will not attract idiots, as idiots are happy. It will only alienate diehard users who keep on using your browser because they have no alternative. From a loved favorite to the least of evils choice. That's what Firefox has become.
  • What’s Happening in OpenStack-Ansible (WHOA) – August 2016
    My goal with these posts is to inform more people about what we’re doing in the OpenStack-Ansible community and bring on more contributors to the project.
  • PowerShell on Linux? No, Thank You [comic]
  • LLVM Might Get An AAP Back-End (Altruistic Processor)
    There's an active proposal to incorporate a back-end into LLVM for AAP, a processor ISA for deeply-embedded Harvard architectures. AAP is designed for FPGA usage and there is an open-source soft-core with commercial deployments also being available. AAP is short for the Altruistic Processor and is described in technical detail here. AAP is said to be an original design but inspired by the OpenRISC / RISC-V projects.
  • UK-French Data Taskforce publishes joint report
    "Invest in and share experiences building core data registers, learning from the French National Address Database experience”; “develop initiatives to bring basic data literacy into primary and secondary education”; and “commission research into algorithmic transparency and accountability” are among the recommendations listed in a report published in July by the joint French-UK Data Taskforce.
  • Tuscany: how to promote the economy of sharing and collaboration
    In June, the region of Tuscany (Italy), in collaboration with Open Toscana and ANCI Toscana, launched a project, the goal of which is to “build a regional policy on the economy of sharing and collaboration”.
  • MS Tries But Just Doesn’t Get FLOSS
    This is what drove me to GNU/Linux so many years ago.
  • Microsoft's maps lost Melbourne because it used bad Wikipedia data
    Microsoft has laid part of the blame for Bing Maps' mis-location of the Australian city of Melbourne by a whole hemisphere on Wikipedia. Yes, Wikipedia, “the free encyclopaedia that anyone can edit.” Microsoft made its admission after your correspondent took to Twitter on Monday to do what we in publishing call “pimping"the story of Melbourne's mis-placement. Ricky Brundritt, a senior program manager at Bing Maps, noticed that pimping and responded as follows.
  • Northern Ireland promotes Open Data in education
    The Northern Ireland Department of Finance has supported a challenge that encourages the re-use of public Open Data in education. Called the OpenDataNI Challenge – Using Open Data for Education” (ODNI4EDU), this project, officially launched on June 14, intends to award two applications or educational tools and resources that make use of at least one dataset published on the portal OpendataNI.
  • Try this handy tool to convert a Web site into a native app with Electron
  • Introducing CloudiumOS [Ed: built on Electron]
    It is a complete multi platform operating system that allows you to manage your documents, access your media files and collaborate with other people on the go. CloudiumOS can work side-by-side with another operating system (either via a VM, a Desktop app or Mobile App) or as a standalone installation.

Opera Data Breach, Security of Personal Data

  • Opera User? Your Stored Passwords May Have Been Stolen
    Barely a week passes without another well-known web company suffering a data breach or hack of some kind. This week it is Opera’s turn. Opera Software, the company behind the web-browser and recently sold to a Chinese consortium for $600 million, reported a ‘server breach incident’ on its blog this weekend.
  • When it comes to protecting personal data, security gurus make their own rules
    Marcin Kleczynski, CEO of a company devoted to protecting people from hackers, has safeguarded his Twitter account with a 14-character password and by turning on two-factor authentication, an extra precaution in case that password is cracked. But Cooper Quintin, a security researcher and chief technologist at the Electronic Frontier Foundation, doesn’t bother running an anti-virus program on his computer. And Bruce Schneier? The prominent cryptography expert and chief technology officer of IBM-owned security company Resilient Systems, won’t even risk talking about what he does to secure his devices and data.

Android Leftovers