associating cperl-mode with Perl code in emacs - emacs

I am trying to associate CPerl mode with Perl source files in emacs (23.1.1 on CentOS 6).
If I include the following line in my .emacs
(defalias 'perl-mode 'cperl-mode)
then CPerl mode will be loaded when a Perl source file is opened.
However, the following line, which seems like ti should work, results in Perl mode being loaded instead:
(add-to-list 'auto-mode-alist '("\\.p[lm]$" . cperl-mode))
There's no error message - it just loads Perl mode instead of CPerl mode.
The reason I'm asking is that I've had some issues using cperl-set-style (works from the emacs menu but not if I add it as a hook to the CPerl mode when it's been aliased to perl-mode) and I wanted to try loading CPerl mode directly.
The statement I'm using in my .emacs to set the indenting style as a hook to CPerl mode is
(eval-after-load "cperl-mode"
add-hook 'cperl-mode-hook (lambda() (cperl-set-style 'C++))))
This obviously has no effect if CPerl mode is not loaded (when I use the auto-mode-alist approach) and does not do the right thing (seems to use GNU indent style) when I load CPerl mode by aliasing it to Perl mode.

You need to use (cperl-set-style "C++") instead of (cperl-set-style 'C++). If you look at the variable cperl-style-alist (e.g. with C-hv) then you will see that the car's consist of strings rather than symbols. It seems unfortunate that your example failed silently rather than raising an error. Most of the time I would want to know that I tried to choose a non-existant style, but there's probably a good reason for it to be the way it is.

M-: (info "(emacs) Choosing Modes") RET
Do your perl scripts start with #!/usr/bin/perl ?
Second, if there is no file variable specifying a major mode, Emacs
checks whether the file's contents begin with `#!'. If so, that
indicates that the file can serve as an executable shell command, which
works by running an interpreter named on the file's first line (the
rest of the file is used as input to the interpreter). Therefore,
Emacs tries to use the interpreter name to choose a mode. For
instance, a file that begins with `#!/usr/bin/perl' is opened in Perl
mode. The variable `interpreter-mode-alist' specifies the
correspondence between interpreter program names and major modes.
The default is perl-mode of course:
ELISP> (assoc "perl" interpreter-mode-alist)
("perl" . perl-mode)
So you would simply use add-to-list again...
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))

Related

How to use the same file extension with different modes?

I have files with a .new extension that contain LaTeX code (they are the output of a program that does things on the original .tex source).
In order to check them I need that they have the LaTeX syntax highlighted and that they can be opened in read-only mode to avoid introducing errors. So I put these lines in my .emacs:
(setq auto-mode-alist
(append '(("\\.new\\'" . read-only-mode)
("\\.new\\'" . latex-mode))
auto-mode-alist))
But it does not work as expected because only the first mode is applied. How can I force emacs to apply both read-only-mode and latex-mode to the same file extension?
An easy solution is to associate your file extension with a custom derived major mode:
(define-derived-mode read-only-latex-mode latex-mode "LaTeX"
"Major mode for viewing files of input for LaTeX."
(read-only-mode 1))
(add-to-list 'auto-mode-alist '("\\.new\\'" . read-only-latex-mode))
The new read-only-latex-mode does everything that latex-mode does (including running latex-mode-hook if you're already using that), but it additionally enables read-only-mode after all of the parent mode's actions have taken place.
Any other code which cares about the major mode being latex-mode should already be testing that using the standard approach of (derived-mode-p 'latex-mode) which is still true for this new mode; so in principle this approach should Just Work.
Not sure I can really provide an answer, but here's some things to consider:
read-only-mode is a minor mode, while
auto-mode-alist is meant to turn on major modes. For major modes, it makes sense that you can have only one.
You can put something like -*- buffer-read-only: t; -*- on your first line in the file (possibly behind some comments or shebangs, depending on the file type) which would turn on read-only-mode. Maybe automate this using auto-insert-alist and match the .new filename pattern.
There's some good suggestions over at https://www.reddit.com/r/emacs/comments/jqxgiz/how_can_i_make_some_files_automatically_readonly/ , namely try file local variables and use find-file-hook
Hope some of that helps.

Emacs openfile in terminal don't load configuration correctly. But open file in emacs do load configuration correctly

Emacs openfile in terminal don't load configuration correctly. But open file in emacs do load configuration correctly.
I have tried several emacs configuration for cpp:
(eval-after-load "cc-vars" '(lambda() (c-set-style "linux")))
Another one
(add-hook 'c-mode-hook '(lambda() (c-set-style "linux")))
Another one
(eval-after-load "cc-vars" '(setq c-default-style "linux")))
If I type emacs filename they can't be loaded. It seems the emacs always load the default gnu style.
After I type M-x c-mode They can be load correctly. Or I open emacs in terminal and then C-x C-f load the file, those configuration will also be loaded.
Is there any reason for this?
I also tried putting
(c-set-style "linux")
in configuration directly.
They work with emacs filename. But when I open non cpp file there will be an error says can't apply c-set-style on that file.
From the CC-mode manual:
When you create a new buffer, its style will be set from c-default-style.
From the Emacs manual
You can also set the variable c-default-style to specify the default style for various major modes
From the documentation of the c-set-style function:
Set the current buffer to use the style STYLENAME.
This means that any call to c-set-style must ensure that the current buffer is the one you want to set the style for; placing such a call at the toplevel in your init file is then definitely a bad idea.
The simplest fix is just to put (setq c-default-style "linux") at the top level, and then wrap it up in an eval-after-load if you really need one. The variable is intended for this, and hooks should be reserved to control more precisely the style of the current file if you need it (as is suggested by c-default-style documentation).
It seems that for me, (eval-after-load "cc-vars" '(setq c-default-style "linux")) works fine: if I open any file covered by CC-mode, the Linux style is applied. Note that c-mode and CC modes are different things, that there exist the c-mode-hook, c++-mode-hook, c-mode-common-hook and they are all different and will run at different times.

Emacs: auto-complete-mode on but no auto-suggestions showing (Emacs lisp mode)

I'm puzzled as to why nothing pops up (in Emacs lisp mode) when I begin typing a function name. For example, after typing (def on a new line, I would assume that auto-complete should be showing me a alist of options which includes defun. Am not sure how long the default delay is, but I waited for a few seconds and nothing happened. Any suggestions?
Details regarding my installation process:
Installed using package-install via Melpa
Added the following two lines to my init.el file:
(require 'auto-complete-config)
(ac-config-default)
Confirmed that load-path includes the folder containing the .el files associated with auto-complete. (I have it set-up to recursively add all folders under path/to/my/.emacs.d/.)
Confirmed (via describe-variable) that ac-dictionary-directories includes the correct directories when Emacs starts up. As reference, it includes the following two directories:
ac-dictionary-directories is a variable defined in 'auto-complete.el'.
Its value is ("/home/dchaudh/Dropbox/dchaudhUbuntu/emacs/.emacs.d/elpa/auto-complete-20140824.1658/dict")
Confirmed that auto-complete-mode is on when I open my init.el file, which obviously triggers emacs-lisp-mode (I can see Emacs Lisp in my mode line). The following is included in the summary of active modes (i.e., via describe-mode):
Global-Auto-Complete minor mode (no indicator)
Toggle Auto-Complete mode in all buffers.
With prefix ARG, enable Global-Auto-Complete mode if ARG is positive;
otherwise, disable it. If called from Lisp, enable the mode if
ARG is omitted or nil.
Not a direct answer, but company works fine out of the box in emacs-lisp-mode, so you might want to try that one.
In my experience, other modes (e.g. flyspell) can interfere with auto-complete operation. (There is a workaround for slyspell built into auto-complete but you have to activate it in your. emacs file.)
I'd suggest trying it with an empty. emacs and then gradually adding parts of your configuration back in. You should be able to find the problem that way.

running scheme from emacs

I'm a newbie to LISP.
I am attempting to invoke the scheme interpreter from within emacs (version 23 running on windows). I loaded the xscheme library by telling emacs to M-x load-library and then entering xscheme at the prompt in the minibuffer. The library loaded, and then I issued the M-x run-scheme command. (I realize that all this loading can be done from .emacs at startup, but I am not concerned with that at the moment.)
So far so good - the *scheme* buffer has been created, and now I'm hoping that I'm able to talk to the scheme interpreter.
However, when I try to evaluate something in that *scheme*buffer (e.g. (define shoe-size 14)), I get this Output file descriptor of scheme is closed message in the minibuffer.
Does anybody know how to fix this in emacs?
(Also, how does one set the major-mode as REPL in the *scheme* buffer?)
Thank you.
Try setting the scheme-program-name variable to the path to your Scheme interpreter. The major-mode of the scheme buffer is probably just comint and you cannot do much about it unless you switch to something more capable like Geiser - something that I'd recommend you do.
Add this line to your .emacs file:
(setq scheme-program-name "gsi")
(Replace "gsi" with the name of your Scheme interpreter.)
You can then start the interpreter with M-x run-scheme. You can evaluate pieces of code by using C-x C-e (to evaluate the sexp before the point) or with C-M-x to evaluate the sexp you're in right now. You can also load a file with C-c C-l.
I'll start by saying that I'm very new to programming, scheme and SICP, but I'm trying to work through the course and watch the lectures that are online.
I'm running emacs on Ubuntu 12.10 and I really wanted to get MIT scheme working in emacs instead of relying on Edwin.
The above tips didn't work for me, but here's the step-by-step instructions that did work:
Install emacs 24 from the Ubuntu Software Center (search "emacs" and install it!)
Open a terminal (ALT + CTRL + t)
Go to your home directory (cd ~)
Open the hidden file .emacs in gedit (gedit .emacs)
On the first line of the file, type exactly what's after the colon: (require 'xscheme)
Save the changes to .emacs
That's it!!!
You can now open .scm files in emacs and use commands like C-x C-e.
*directions courtesy of http://alexott.net/en/writings/emacs-devenv/EmacsScheme.html#sec14
My guess is that it's just a known issue I still dunno how to sort that out (it's out of my current skills) but I got a macro that probably helps: just after writing the s-exp you can do Cc-Cz (it calls the geiser REPL) then C-spc, C-M-b, M-w, C-x-o, C-y and RET.
There are a variation (same, placed just after writing the s-exp): C-spc, C-M-b, M-w, C-c Cz, C-y and RET

How do I set Aquamacs to use Prolog mode?

I'm new to Emacs/Aquamacs, and want to use it to program in Prolog. My filename ends with ".pl" and Aquamacs automatically assumes it's a Perl file. How do I change it to use Prolog mode instead? I'd prefer an answer that doesn't assume I know how to get around in Emacs at all.
Under Preferences -> Programming -> Languages. I cannot find Prolog, even though the Aquamacs webpage states it's supposed to support Prolog? Do I need to install some addon?
You can activate Prolog mode manually with M-x prolog-mode.
I have the following line in my Emacs configuration file (usually referred to as .emacs, though I personally use the alternative location .emacs.d/init.el) to make it the default for .pl files:
(add-to-list 'auto-mode-alist '("\\.pl\\'" . prolog-mode))