Launching mono exe file within emacs/Fsharp mode - emacs

I'm using Fsharp mode in emacs.
The key of ^C x is mapped to Run ... command which is as follows.
(defun fsharp-run-executable-file ()
(interactive)
(let ((name (buffer-file-name)))
(if (string-match "^\\(.*\\)\\.\\(fs\\|fsi\\)$" name)
(shell-command (concat (match-string 1 name) ".exe")))))
The problem is that it tries to run bash something.exe, whereas I need to run the command of mono something.exe. I got error message of /bin/bash ...exe: cannot execute binary file.
How can I come up with a new elisp command to launch mono, and then get the result to show it to *compilation* buffer?

You could try changing the last line to:
(shell-command (concat "mono " (match-string 1 name) ".exe")))))
but I haven't tested this.

You can redefine fsharp-run-executable-file and use this one instead:
(defun fsharp-run-executable-file ()
(interactive)
(let ((name (buffer-file-name)))
(if (string-match "^\\(.*\\)\\.\\(fs\\|fsi\\)$" name)
(compile (concat "mono " (match-string 1 name) ".exe")))))
There are two changes: 1) concat mono before the command (as petebu wrote); 2) use the compile function so that the output is in the *compilation* buffer.
To test quickly, just evaluate the above function (add it in your Emacs init file for a permanent change). Note that you shouldn't modify fsharp.el file, at I might update at some point (you don't want to lost your changes).
Edit
One issue with the previous function is that it modifies the last compilation command. This might might annoying if you compile your code with compile or recompile commands. Here is a fix:
(defun fsharp-run-executable-file ()
(interactive)
(let ((name (buffer-file-name)))
(if (string-match "^\\(.*\\)\\.\\(fs\\|fsi\\)$" name)
(compilation-start (concat "mono " (match-string 1 name) ".exe")))))

Related

Update gtag file in emacs for a single file once on saving

My configuration is copied from http://www.emacswiki.org/emacs/GnuGlobal#toc4 . The purpose is to update existing tag file once i save source file. But the result is the same as expected.
The full configuration is:
(gtags-mode 1)
(defun gtags-root-dir ()
"Returns GTAGS root directory or nil if doesn't exist."
(with-temp-buffer
(if (zerop (call-process "global" nil t nil "-pr"))
(buffer-substring (point-min) (1- (point-max)))
nil)))
(defun gtags-update-single(filename)
"Update Gtags database for changes in a single file"
(interactive)
(start-process "update-gtags" "update-gtags" "bash" "-c" (concat "cd " (gtags-root-dir) " ; gtags --single-update " filename )))
(defun gtags-update-current-file()
(interactive)
(defvar filename)
(setq filename (replace-regexp-in-string (gtags-root-dir) "." (buffer-file-name (current-buffer))))
(gtags-update-single filename)
(message "Gtags updated for %s" filename))
(defun gtags-update-hook()
"Update GTAGS file (insert )ncrementally upon saving a file"
(when gtags-mode ;;It is copy past error..
(when (gtags-root-dir)
(gtags-update-current-file))))
(add-hook 'after-save-hook 'gtags-update-hook)
Update
My understanding is that tags will be update via command
(gtags-update-single filename)
(message "Gtags updated for %s" filename))
once file in buffer is saved. That means a new added or renamed or removed function will be updated to the tag file. In my test, I do see output message(The tags is in ededemo directory ):
Wrote /other/projectbase/cplusproject/ededemo/src/main.cpp
Gtags updated for ./src/main.cpp
each time function is renamed or added after c-x c-s. But M-x gtags-find-tag could not find my new added function. Is there any wrong in understanding?
This line is clearly responsible/broken:
(when gtags-;-*- mode: ${1:mode} -*-
Looking at the Wiki page, I can't fathom how you managed to end up with that.
The docstring comment is also corrupted. Just copy the whole function again.

Going root when writing to file/saving file?

Is it possible to open a file(in root location) as non-root user in Emacs, edit it and then when its time to save provide the password so the Emacs can get write to the file? Better still provide different buffers with different user privileges?
I know of Tramp but couldn't get my head around it.
Here's how I do it:
(require 'tramp)
(defun sudired ()
(interactive)
(dired "/sudo::/"))
You'll get a dired buffer where you have root privileges.
Any subsequent directory or file that you open from here will be with root.
Any other dired buffers will not be affected.
Update: I now use sudo-edit (available on Melpa or at https://github.com/nflath/sudo-edit), which has the header warning and is more robust than this function.
This is what I use. You can open a file (even one that doesn't exist yet) or directory as a normal user, and run this function to get root privileges.
(defun find-alternative-file-with-sudo ()
(interactive)
(let ((bname (expand-file-name (or buffer-file-name
default-directory)))
(pt (point)))
(setq bname (or (file-remote-p bname 'localname)
(concat "/sudo::" bname)))
(cl-flet ((server-buffer-done
(buffer &optional for-killing)
nil))
(find-alternate-file bname))
(goto-char pt)))
I also have this, which makes a big red banner across the top of the buffer telling me it's opened as root.
(defface find-file-root-header-face
'((t (:foreground "white" :background "red3")))
"*Face use to display header-lines for files opened as root.")
(defun find-file-root-header-warning ()
"*Display a warning in header line of the current buffer.
This function is suitable to add to `find-file-hook'."
(when (string-equal
(file-remote-p (or buffer-file-name default-directory) 'user)
"root")
(let* ((warning "WARNING: EDITING FILE AS ROOT!")
(space (+ 6 (- (window-width) (length warning))))
(bracket (make-string (/ space 2) ?-))
(warning (concat bracket warning bracket)))
(setq header-line-format
(propertize warning 'face 'find-file-root-header-face)))))
(add-hook 'find-file-hook 'find-file-root-header-warning)
(add-hook 'dired-mode-hook 'find-file-root-header-warning)
You don't need any special functions for this, it's built-in to Emacs (at least it is for version 24).
To open a file as root:
C-x C-f to open the find-file dialog in the minibuffer.
Then prepend /su::/ to the file path:
/su::/path/to/root/file
You'll be prompted for the root password. After that, you can open the file as if you are root. The rest of your buffers will be unaffected. However, if you open another file from the same buffer, you'll automatically be opening it as root.
I wanted to have a way to open root files too, so I came up with this function that replaced build in find-file, now I have this in my .emacs:
(defun test (&rest args)
(with-temp-buffer
(eq (apply 'call-process "test" nil (current-buffer) nil args) 0)))
(defun have-permission (filename)
;; only bash expand ~ with home directory
(let ((expanded (replace-regexp-in-string "~"
(concat "/home/" (user-real-login-name))
filename)))
(if (not (file-exists-p expanded))
(let ((directory (file-name-directory expanded)))
(and (test "-r" directory) (test "-x" directory) (test "-w" directory)))
(and (test "-r" expanded) (test "-w" expanded)))))
(defun find-every-file (filename &optional wildcards)
"Open file use sudo:: if user have no permissions to open the file"
(interactive
(find-file-read-args "Find All Files: "
(confirm-nonexistent-file-or-buffer)))
(find-file (if (have-permission filename)
filename
;; you can replace that with /su:: if you don't have sudo access
(concat "/sudo::" (file-truename filename)))))
(global-set-key (kbd "C-x C-f") 'find-every-file)
It also work if you try to open non existing file or non existing file in directory you don't have write permissions.
you can combine it with #jpkotta warning popup.

Waiting on compilation to finish

I am using compile to pull new files from source tree using mercurial "hg pull".
I am performing a save of all buffers before the pull and would like to "refresh all opened buffers" after the compilation "pulling" finishes.
I tried experimenting with compilation-finish-functions but found out that the functions added to the list will be executed after "every" compilation. Since I use compile to search IDs "gid" I don't want to refresh opened files on every search.
How can I wait on compilation to finish before refreshing opened files "only" while inside a command and not on every compile outside of the command.
Here is the code:
; From http://www.emacswiki.org/emacs/CompileCommand
(defun compile-pkg (&optional command startdir)
"Compile a package, moving up to the parent directory
containing configure.ac, if it exists. Start in startdir if defined,
else start in the current directory."
(interactive)
(let ((dirname) (dir-buffer nil))
(setq startdir (expand-file-name (if startdir startdir ".")))
(setq command (if command command compile-command))
(setq dirname (upward-find-file "Makefile" startdir))
; (setq dirname (if dirname dirname (upward-find-file "Makefile" startdir)))
; (setq dirname (if dirname dirname (expand-file-name ".")))
; We've now worked out where to start. Now we need to worry about
; calling compile in the right directory
(save-excursion
(setq dir-buffer (find-file-noselect dirname))
(set-buffer dir-buffer)
(compile command)
(kill-buffer dir-buffer)
)))
(defun upward-find-file (filename &optional startdir)
"Move up directories until we find a certain filename. If we
manage to find it, return the containing directory. Else if we
get to the toplevel directory and still can't find it, return
nil. Start at startdir or . if startdir not given"
(let ((dirname (expand-file-name
(if startdir startdir ".")))
(found nil) ; found is set as a flag to leave loop if we find it
(top nil)) ; top is set when we get
; to / so that we only check it once
; While we've neither been at the top last time nor have we found
; the file.
(while (not (or found top))
; If we're at / set top flag.
(if (string= (expand-file-name dirname) "/")
(setq top t))
; Check for the file
(if (file-exists-p (expand-file-name filename dirname))
(setq found t)
; If not, move up a directory
(setq dirname (expand-file-name ".." dirname))))
; return statement
(if found (concat dirname "/") nil)))
(defun compile-hgpull ()
(interactive)
(save-all-buffers)
(compile-pkg "hg pull -u")
; if (compile finished) -> (revert-all-buffers)
)
(global-set-key [f1] 'compile-hgpull)
compile is async. So, you have two choices.
One, don't use compile. Instead use one of the other ways to invoke a shell command, like shell-command or start-process or call-process. I think this is probably preferred; I don't see why you'd need to use compile here.
Two, set compilation-finish-function.
If you want to run a shell-command synchronously, and then see its output, it might be easier to use shell-command-to-string than compile.

Using Emacs, is it possible to pin the compilation command to a specific buffer/directory?

Right now I am using the following to compile, when I'm in for example main.cpp
C-x b Makefile RET M-x compile RET RET
I actually have M-x compile as a keyboard shortcut, but the problem is I would really like not having to go through all that trouble to simply run my Makefile.
I need to visit Makefile to make sure the compile command is executed using the same directory. Is there any way to pin the directory so I can simply go M-x compile RET RET?
Best regards
Use recompile instead. C-u M-x recompile will let you edit the compile command first. Either way the compile will work out of the directory the last compile was done in.
See my answer here
Directory local variables provide an easy way to trigger the compile from a parent directory of any source file in a subdirectory.
I run emacs primarily on windows.
When I have a makefile that is in a parent directory of a C module, I use this as the compile command:
cd .. && nmake <arguments here>
for example:
cd .. && nmake CONFIG=Debug PLATFORM=x64 target
Beyond that, I find that specifying the make command line that I want to run for various modules is sort of a pain. I wanted a way to attach the default compile command to the buffer being edited. So I wrote a little elisp to handle that job. I figured to insert into the header comments of each buffer a line that would stipulate my preferred compile command, like this:
compile: cd .. && nmake CONFIG=Debug PLATFORM=x64 target
And then have a piece of elisp run, before I invoke M-x compile that grabs the line and proposes it as the compile command I would like to run.
This defun pulls a line out of the header comments:
(defun cheeso-c-get-value-from-comments (marker-string line-limit)
"gets a string from the header comments in the current buffer.
This is used to extract the compile command from the comments. It
could be used for other purposes too.
It looks for \"marker-string:\" and returns the string that
follows it, or returns nil if that string is not found.
eg, when marker-string is \"compile\", and the following
string is found at the top of the buffer:
compile: cl.exe /I uthash
...then this command will return the string
\"cl.exe /I uthash\"
It's ok to have whitespace between the marker and the following
colon.
"
(let (start search-limit found)
;; determine what lines to look in
(save-excursion
(save-restriction
(widen)
(cond ((> line-limit 0)
(goto-char (setq start (point-min)))
(forward-line line-limit)
(setq search-limit (point)))
((< line-limit 0)
(goto-char (setq search-limit (point-max)))
(forward-line line-limit)
(setq start (point)))
(t ;0 => no limit (use with care!)
(setq start (point-min))
(setq search-limit (point-max))))))
;; look in those lines
(save-excursion
(save-restriction
(widen)
(let ((re-string
(concat "\\b" marker-string "[ \t]*:[ \t]*\\(.+\\)$")))
(if (and start
(< (goto-char start) search-limit)
(re-search-forward re-string search-limit 'move))
(buffer-substring-no-properties
(match-beginning 1)
(match-end 1))))))))
Ok, now I need something to invoke that before I invoke compile.
(defun cheeso-invoke-compile-interactively ()
"fn to wrap the `compile' function. This simply
checks to see if `compile-command' has been previously set, and
if not, invokes `cheeso-guess-compile-command' to set the value.
Then it invokes the `compile' function, interactively."
(interactive)
(cond
((not (boundp 'cheeso-local-compile-command-has-been-set))
(cheeso-guess-compile-command)
(set (make-local-variable 'cheeso-local-compile-command-has-been-set) t)))
;; local compile command has now been set
(call-interactively 'compile))
Then of course, the defun that guesses the compile command:
(defun cheeso-guess-compile-command ()
"set `compile-command' intelligently depending on the
current buffer, or the contents of the current directory."
(interactive)
(set (make-local-variable 'compile-command)
(cond
(buffer-file-name
(let ((filename (file-name-nondirectory buffer-file-name)))
(cond
;; editing a C-language source file - check for an
;; explicitly-specified command
((string-equal (substring buffer-file-name -2) ".c")
(let ((explicit-compile-command
(cheeso-c-get-value-from-comments "compile" 34)))
(or explicit-compile-command
(concat "nmake " ;; assume a makefile exists
(file-name-sans-extension filename)
".exe"))))
;; editing a makefile - just run nmake
((string-equal (substring buffer-file-name -8) "makefile")
"nmake ")
;; something else - do a typical .exe build
(t
(concat "nmake "
(file-name-sans-extension filename)
".exe")))))
(t
;; punt
"nmake "))))
The final bit is to bind C-x C-e , normally bound to compile, to the wrapper defun:
(global-set-key "\C-x\C-e" 'cheeso-invoke-compile-interactively)
Now, when I do C-x C-e in the buffer, it searches for the compile command, and proposes to me the command that it finds. I can edit the proposed compile command, then press ENTER and run it.

Why is my term-mode-hook not selecting line mode?

I wrote this elisp function:
(defun run (command)
"Open a terminal running a command."
(interactive "sCommand: ")
(if (buffer-exists (concat "*" command "*" )) (kill-buffer (concat "*" command "*")))
(let ((term-mode-hook (cons (lambda () (term-line-mode)) term-mode-hook)))
(ansi-term (cons "sh" (cons "-i" (list "-c" command))) command)))
This works nicely except that the new ansi-term buffers remains in char mode (which is the default), so as far as I can tell the term-line-mode call is not doing anything. If I replace (term-line-mode) with (message "foo") I do see the message in the messages buffer.
The definition of term-line-mode in lisp/term.el is:
(defun term-line-mode ()
"Switch to line (\"cooked\") sub-mode of term mode.
This means that Emacs editing commands work as normally, until
you type \\[term-send-input] which sends the current line to the inferior."
(interactive)
(when (term-in-char-mode)
(use-local-map term-old-mode-map)
(term-update-mode-line)))
What am I doing wrong?
I wasn't able to get "term-line-mode" to work as you want in any of the term hooks; however, it does work if you advise the "ansi-term" function:
(defadvice ansi-term (after advice-term-line-mode activate)
(term-line-mode))