Tuareg-mode and caml-mode - emacs

I'm currently using tuareg-mode but I would like to be able to use the functionality of caml-mode as well. In particular I want to be able to use type annotations interactively, and apparently this occurs with caml-types. I tried putting http://cristal.inria.fr/~remy/poly/emacs/index.html in my .emacs.d, but I'm confused about how or if these two modes can work together. In fact, I can't get caml-mode to work at all.
I have this line in my init.el:
(add-to-list 'load-path "~/.emacs.d/modes/caml")
But the files are not loaded - at least, none of the function definitions or keybindings are. I really thought I was starting to grasp how these emacs plugins work, but I'm starting to wonder. Maybe someone can explain what else needs to happen?
Edit: I didn't realize I had to require 'caml for this to work. Still, annotations don't seem to be working although I have caml-types from http://caml.inria.fr/svn/ocaml/branches/gadts/emacs/. I compile with -annot but I'm still told there's no annotations file.

You can have type annotation with the tuareg mode.
If I have this exact ~/.emacs file:
(add-hook 'tuareg-mode-hook '(lambda ()
(define-key tuareg-mode-map [f10] 'caml-types-show-type); requires caml-types
))
(add-to-list 'auto-mode-alist '("\\.ml\\w?" . tuareg-mode))
(autoload 'caml-types-show-type "caml-types" "Show the type of expression or pattern at point." t)
then pressing F10 shows the type of the expression under the point.
As you know, you need to compile your file foo.ml with
ocamlc -annot foo.ml
so that there is a file foo.annot in the same directory as foo.ml.

Related

How do I make an interactive command automatically available?

I feel like this question is super basic, but I haven't been able to figure out how to automatically make a simple interactive command available in an Emacs session...
This is in ~/random/exploration/exploration.el.
;;; Code:
;;;###autoload
(defun exploration ()
"a test package"
(interactive)
(message "hi"))
(provide 'exploration)
;;; exploration.el ends here
This is in init.el:
(add-to-list 'load-path (expand-file-name "~/random/exploration"))
exploration isn't available via M-x though. I have to do M-: (require 'exploration) before it appears.
How would I make this command available automatically, like plugins do? I've been poring over the docs for load-path and autoload but can't figure out how to make this happen.
I want to do this so I can put other functions in exploration.el and have them only available after the user first does M-x exploration. That implies that exploration needs to be autoloaded.
Comments on any redundancies in what I've done here, or tips on how I could have debugged this on my own would also be welcome.
The ;;;###autloload comment is just a comment. When Emacs is built (or a suitably intelligent package manager installs your code as a package) it generates an actual autoload, but outside of that, you need to do it yourself.
(autoload 'exploration "exploration" nil t)
Specify an explicit path in the second parameter and you won't actually need the load-path manipulation to accomplish what you describe.
See also https://www.gnu.org/software/emacs/manual/html_node/eintr/Autoload.html
You have to either load the file instead of adding it to the load-path or you need to put (require 'exploration) to your init.el file after adding the folder to the load-path.
Use autoload function:
(autoload 'exploration "exploration")
If you don't modify load-path, you need an absolute path as the second argument.
Alternatively, install your script as a package, either through MELPA (assuming you can get it there), or locally with package-install-file. This will take care about autoloads for you.

Use only two functions from the evil package as keybinding

I want to get the search at point functionality ("*" and "#") in emacs.
So I want to use "*" and "#" from the emacs evil-mode, as this is one of the suggestions. However, I don't want anything else from the evil-mode, just those two functions!
This is my .emacs file:
(package-initialize)
(evil-mode 1) ;; enable evil-mode
(global-set-key (kbd "C-*") 'evil-search-symbol-forward)
(global-set-key (kbd "C-#") 'evil-search-symbol-backward)
Now the keybindings work, but I loaded the whole evil-mode, so it messes up my standard emacs keybindings like "C-y" for yank.
If I don't load the evil-mode in the .emacs file, I get the error:
Symbol's function definition is void: evil-search-symbol-forward
#event_jr's answer mentions highlight-symbols, which is probably a much more lightweight way to do what you want.
However, if you really want to use the evil version, you can (require 'evil) in your .emacs file without turning it on (ie, leave off the (evil-mode 1) statement).
Meanwhile, the functions you want are actually named evil-search-word-forward and evil-search-word-backward, not the ...-symbol-... version you saw on the wiki page (which is probably outdated).
You should use highlight-symbols for symbol jumping and highlighting.

Running a .el file when opening emacs

I downloaded two .el files
One is to highlight current column where cursor is and another one to highlight some specific words.
I followed next steps in the file:
(add-to-list 'load-path "~/.xemacs/packages/") //path where I saved .el files
(load "column-marker") //name of file without .el extension
To this step it works fine, I find those when I press M-x column-marker and I can use it.
My problem begins when I want to use it everytime i open emacs and I found that I could use something similar to this:
(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
Which in my case I am typing:
(add-to-list 'auto-mode-alist '("\\.\\'" . column-marker)) //I want to enable it for all .something files
But... when doing that, there is an error when opening emacs and it starts with default setup.
What am I doing wrong? I have tried many ways and none work.
Thanks in advance
Always post the error message you see. That helps others help you.
C-h f auto-mode-alist tells you that the functions you use in it must implement major modes. column-marker is not a major-mode function. It is not even a function; it is a file.
What you need to do, for each mode where you want some function in file column-marker.el to be invoked, is to put that function on the major-mode hook for that function. For example:
(add-hook 'emacs-lisp-mode (lambda () (interactive) (column-marker-1 80))
And lo and behold, what does the Commentary in file column-marker.el tell you?
;; Installation: Place this file on your load path, and put this in
;; your init file (`.emacs'):
;;
;; (require 'column-marker)
;;
;; Other init file suggestions (examples):
;;
;; ;; Highlight column 80 in foo mode.
;; (add-hook 'foo-mode-hook (lambda () (interactive) (column-marker-1 80)))
Couldn't be clearer. Provided you actually read it.
Seriously, a minimum of investigation is in order, before you post a question to StackOverflow. You should do a Google search, open README files and read them, and so on --- first. And in the case of Emacs questions, IMHO, you should ask Emacs first (e.g. C-h v auto-mode-alist).
According to the rules of S.O. posting, not doing preliminary simple research is even grounds for closing a question. So do not be surprised if this question gets closed or downvoted.

What happened to the ido-imenu in ruby-mode function in Emacs24?

Emacs 23.2 in emacs-starter-kit v1 has C-x C-i (or ido-imenu) (similar to Sublime Text's Cmd+R). Emacs24 in emacs-starter-kit v2 lacks this function. I found this github issue and a fix, which try to recreate the functionality. While this ido-imenu works in elisp-mode, it stopped working in ruby-mode. I get:
imenu--make-index-alist: No items suitable for an index found in this buffer
Has anyone figured out how to get this to work?
Why was this taken out of Emacs24?
Is there a new replacement for this function?
Since the function is part of ESK (as opposed to something budled with Emacs) you'd probably do best to report the bug upstream. On a related note ESK main competitor Emacs Prelude offers the same functionality (bound to C-c i by default) and it seems to be working fine with ruby-mode in Emacs 24. Here you can find more on ido-imenu.
So I finally figured it out, after reading the Defining an Imenu Menu for a Mode section on emacs-wiki again.
Short answer: you need to add this bit to your customization. Feel free to add more types to the list (I am happy with just methods).
(add-hook 'ruby-mode-hook
(lambda ()
(set (make-local-variable imenu-generic-expression)
'(("Methods" "^\\( *\\(def\\) +.+\\)" 1)
))))
Longer answer: I first tried to define a ruby-imenu-generic-expression function and set that to imenu-generic-expression by using the ruby-mode-hook:
(defvar ruby-imenu-generic-expression
'(("Methods" "^\\( *\\(def\\) +.+\\)" 1))
"The imenu regex to parse an outline of the ruby file")
(defun ruby-set-imenu-generic-expression ()
(make-local-variable 'imenu-generic-expression)
(make-local-variable 'imenu-create-index-function)
(setq imenu-create-index-function 'imenu-default-create-index-function)
(setq imenu-generic-expression ruby-imenu-generic-expression))
(add-hook 'ruby-mode-hook 'ruby-set-imenu-generic-expression)
This however did not work (I would get the same error as before). More reading of the Defining an Imenu Menu for a Mode section showed me the way. Now, I'm not an elisp expert, so here's my hypothesis: basically, the above method works for modes where the
major mode supports a buffer local copy of the “real” variable, ‘imenu-generic-expression’. If your mode doesn’t do it, you will have to rely on a hook.
The example for foo-mode made it clear how to do it for ruby-mode. So it appears that ruby-mode does not have a buffer-local copy of the real imenu-generic-expression variable. I still can't explain why it worked in Emacs 23.2 (with ESK v1) but does not on Emacs24, but hey at least I found a working solution.

Auto-complete mode doesn't turn on automatically in ObjC buffers

I load auto-complete mode like this:
(let ((ac-path "path/to/auto-complete"))
(add-to-list 'load-path ac-path)
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories (concat ac-path "ac-dict"))
(ac-config-default))
It works fine with C major mode, but doesn't turn on automatically when I open ObjC files. I can still turn it on manually and it will work fine along with the ObjC major mode.
Here's a snippet from the docs regarding ObjC major mode:
The hook `c-mode-common-hook' is run with no args
at mode initialization, then `objc-mode-hook'.
If I understand correctly, auto-complete adds a hook to the c-mode-common-hook, but objc-mode-hook somehow overrides it. Is there a way to fix this?
Thanks.
While looking through the source code of auto-complete.el, I've stumbled upon this definition
(defcustom ac-modes
'(emacs-lisp-mode
lisp-interaction-mode
c-mode cc-mode c++-mode
java-mode clojure-mode scala-mode
scheme-mode
ocaml-mode tuareg-mode
perl-mode cperl-mode python-mode ruby-mode
ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode
makefile-mode sh-mode fortran-mode f90-mode ada-mode
xml-mode sgml-mode)
"Major modes `auto-complete-mode' can run on."
:type '(repeat symbol)
:group 'auto-complete)
It turns out that auto-complete doesn't have a true global mode. It is enabled only with those major modes that are included in the ac-modes variable.
So, adding the following line to the .emacs file has solved the issue for me.
; add this line after the auto-complete mode has been loaded
(add-to-list 'ac-modes 'objc-mode)
Use the following:
(defun my-objc-mode-hook ()
(auto-complete-mode 1))
(add-hook 'objc-mode-hook 'my-objc-mode-hook)
Note 1: The function auto-complete-mode is a toggle function, when called with no arguments.
Note 2: It's possible to add an anonymous function using lambda, but this have several drawbacks. The most important ones are: modifying the function and reevaluating the expression will add the modified function in addition to the earlier version and C-h v xxx will print the full unformatted lambda function, which typically is hopeless to read and understand.
(add-hook 'objc-mode-hook 'auto-complete-mode)
That should do it if you're using auto-complete-mode. You can add more complex things to mode hooks by doing:
(add-hook 'objc-mode-hook '(lambda ()
(something-with arguments)))
Note that both arguments to add-hook are quoted, this is necessary and if you add unquoted functions they will probably not work.