Emacs: Friend or Foe?
One problem with the above configuration is that font-lock-mode is not automatically enabled for each major mode. Including the font-lock-mode command in your startup.el file will enable this mode when Emacs first begins, but not for each new buffer that you create. In general, this is true for any Emacs minor mode; you must turn on the minor modes whenever you enter a new major mode.
What we want to do is have the font-lock-mode command be executed whenever we enter certain modes, such as C mode or Emacs LISP mode. Happily, Emacs provides “hooks” which allow you to execute functions when certain events occur.
Let's turn on several minor modes whenever we enter C mode, Emacs LISP mode, or Text mode:
(defun my-enable-minor-modes () "Enables several minor modes." (interactive "") (transient-mark-mode 1) (font-lock-mode 1)) (add-hook 'c-mode-hook 'my-enable-minor-modes) (add-hook 'emacs-lisp-mode-hook 'my-enable-minor-modes) (add-hook 'text-mode-hook 'my-enable-minor-modes)
Now you should find that upon entering any of these major modes, the corresponding minor modes are enabled as well. In general, each major mode has an entry hook, named modename-hook.
Instead of using global-set-key to define key bindings for all major modes, we can use define-key to set bindings for particular modes. In this way we can specify the behavior of certain keys based on the major mode that you happen to be in.
For example, I prefer that the return key indent the next line of text relative to the one above it (if the previous line is indented five spaces, the next line should be as well). To enable this feature in C mode, Emacs LISP mode, and Indented Text Mode, use the commands:
(define-key indented-text-mode-map "\C-m" 'newline-and-indent) (define-key emacs-lisp-mode-map "\C-m" 'newline-and-indent) (define-key c-mode-map "\C-m" 'newline-and-indent)
Each mode has a modename-map associated with it, which specifies the key bindings for that mode. As you may have guessed, newline-and-indent is the Emacs function which does a newline followed by a relative indent.
When editing files, Emacs usually determines the mode to use from the filename extension. If I were to edit a new file called clunker.c, C mode would be used as the default. However, when unable to make a decision, Emacs uses Fundamental mode. I prefer to use Indented Text mode instead, which is enabled with the command:
(setq default-major-mode 'indented-text-mode)
Local key bindings can be used for more interesting tasks than those demonstrated above. For example, the command M-x compile will issue the command make -k (by default) in the current directory, thereby compiling whatever code you may have been working on. Output and error messages from the make command are displayed in a separate window. You can select error messages from the compilation buffer, in which case Emacs will automatically open up the corresponding source file and jump to the line containing the error. In all, this makes editing, compiling, and debugging programs much more efficient; you can do literally everything within Emacs.
In order to automate the process of saving the current source file and issuing make, we can bind the key sequence C-c C-c to a new function; let's call it my-save-and-compile. The code looks like this:
(defun my-save-and-compile () "Save current buffer and issue compile." (interactive "") (save-buffer 0) (compile "make -k")) (define-key c-mode-map "\C-c\C-c" 'my-save-and-compile)
The save-buffer command is used to save the current source file, and compile is issued with the command make -k. Now, with two simple keystokes you can fire your source file off to the compiler, and wait for the error messages to roll in. Without the my-save-and-compile function, you have to save the source file (using C-x C-s) and issue M-x compile by hand.
Of course, to use this feature you'll have to create a Makefile in the directory where your source files live. (The make command is issued in the directory containing the source file in question.) Creating Makefiles is another issue altogether. A future issue of Linux Journal will discuss this subject, but in the meantime there are several sources of information about make. The book Managing Projects with make from O'Reilly and Associates is a good place to start, as is the GNU make Manual, which covers the version of make available on Linux systems.
Also note that compile will first prompt you to save any modified buffers. If you modify only one buffer at a time, my-save-and-compile saves it for you. We could have my-save-and-compile save all modified buffers, but you may not want that to happen automatically behind your back.
As we mentioned, the compile function will open a new window containing messages from the make command. From this buffer, you can select error messages for Emacs to automatically jump to, allowing you to fix the problem and move on. If you are running Emacs under X, clicking on an error message with the second mouse button will take you to the line containing the error. Otherwise, you can move point to the error message (in the compilation buffer), and use C-c C-c (don't be confused by the multiple meanings of this key sequence). Alternately, you can use the function next-error to visit the next error message. In my startup.el I have this function bound to M-n in C mode (which, by now, you should know how to do).
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Tech Tip: Really Simple HTTP Server with Python
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Non-Linux FOSS: Caffeine!
- Managing Linux Using Puppet
- Returning Values from Bash Functions
- Doing for User Space What We Did for Kernel Space
- Rogue Wave Software's Zend Server
- Google's SwiftShader Released
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide