How to make functions respect buffer local variables - elisp

In the (amateurish, convoluted) code below, I am trying create an environment where there may be multiple serial-term buffers/windows at the same time. I am doing everything I can think of (and just random desperate things) to make the variables local to the buffer running the terminal to which they pertain. For instance, there may be a buffer "serial-1a2b-buffer" with the terminal "serial-1a2b-term" running in it while at the same time there's a buffer "serial-3c4d-buffer" with the terminal "serial-3crd-term" running in it.
I can get the buffers/terminals setup and running with defun setupserial, but defun killserial and defun resetserial don't get the right value for "termname" and "buffname". It might be the values in the other buffer or it may be past values for buffers and terminals that no longer exist.
(In case anyone is wondering, I do a lot of work with microcontrollers. If the serial connection to them is interrupted, like with a hardware reset, then the serial process dies. The idea was to have a quick way to reset the connection - like with a function bound to a key sequence.)
(defvar serialspeed "115200")
(defvar serialport "/dev/ttyACM0")
(defvar serialbasename "serial")
(require 'term)
(defun setupserial (serialport serialspeed)
(interactive
(list
(read-string
(format "Serial Port (%s): "
serialport)
nil nil
serialport)
(read-string
(format "Speed (%s): "
serialspeed)
nil nil
serialspeed)))
(setq uniqueid (format "%04x" (random (expt 16 4))))
(setq serialid (concat serialbasename "-" uniqueid))
(setq buffname (concat serialid "-buffer"))
(setq termname (concat serialid "-term"))
(setq bufferid (get-buffer-create buffname))
(setq procid (make-serial-process
:speed (string-to-number serialspeed)
:port serialport
:name termname
:buffer buffname))
(switch-to-buffer bufferid)
(make-local-variable 'serialid)
(make-local-variable 'buffname)
(make-local-variable 'bufferid)
(make-local-variable 'termname)
(make-local-variable 'procid)
(make-local-variable 'serialspeed)
(make-local-variable 'serialport)
(term-mode)
(term-char-mode)
(local-set-key (kbd "M-r") #'resetserial)
(local-set-key (kbd "M-k") #'killserial)
(local-set-key (kbd "M-x") #'execute-extended-command)
(local-set-key (kbd "M-o") #'ace-window)
(message "Started Serial Terminal"))
(defun resetserial ()
(interactive)
(make-serial-process
:speed (string-to-number serialspeed)
:port serialport
:name termname
:buffer bufferid)
(message "Restarted Serial Terminal"))
(defun killserial ()
(interactive)
(delete-process termname))
(global-set-key (kbd "C-c s") #'setupserial)
(provide 'setup-serial)

Your problems are sequential. Having created all of your buffer-local variables, you are then destroying them all by calling a new major mode.
The section on "Derived modes, and mode hooks" in this answer might be useful reading, but the key point is that the first thing that happens when you call a major mode is kill-all-local-variables.
Because you are setting global values too, in the absence of local values your other commands will end up using whatever the most-recent global value happened to be.
Set the major mode first.

Related

Something Enables scroll-lock-mode in Emacs

I have Emacs running in WSL2
GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw scroll bars) of 2021-07-20
About couple of weeks ago open buffers started getting scroll-lock-mode enabled. I have it set to nil by default. Moreover, if I disable scroll-lock-mode after some time it gets enabled. I don't have anything anywhere in the configuration related to scrolling. I have instrumented scroll-lock-mode function, but when it jumps in it, seems to be called from random spots
(let ((last-message (current-message))) (setq scroll-lock-mode (cond ((eq arg 'toggle) (not scroll-lock-mode)) ((and (numberp arg) (< arg 1)) nil) (t t))) (when (boundp 'local-minor-modes) (setq local-minor-modes (delq 'scroll-lock-mode local-minor-modes)) (when scroll-lock-mode (push 'scroll-lock-mode local-minor-modes))) (if scroll-lock-mode (progn (setq scroll-lock-preserve-screen-pos-save scroll-preserve-screen-position) (setq-local scroll-preserve-screen-position 'always)) (setq scroll-preserve-screen-position scroll-lock-preserve-screen-pos-save)) (run-hooks 'scroll-lock-mode-hook (if scroll-lock-mode 'scroll-lock-mode-on-hook 'scroll-lock-mode-off-hook)) (if (called-interactively-p 'any) (progn nil (unless (and (current-message) (not (equal last-message (current-message)))) (let ((local " in current buffer")) (message "Scroll-Lock mode %sabled%s" (if scroll-lock-mode "en" "dis") local))))))
scroll-lock-mode(toggle)
funcall-interactively(scroll-lock-mode toggle)
call-interactively#ido-cr+-record-current-command(#<subr call-interactively> scroll-lock-mode nil nil)
apply(call-interactively#ido-cr+-record-current-command #<subr call-interactively> (scroll-lock-mode nil nil))
call-interactively(scroll-lock-mode nil nil)
command-execute(scroll-lock-mode)
Here's the sample of the Messages buffer
Scroll-Lock mode enabled in current buffer
Scroll-Lock mode disabled in current buffer
GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw scroll bars) of 2021-07-20
Mark set
Scroll-Lock mode enabled in current buffer
How to I make it disabled for good ? I used (setq-default scroll-lock-mode nil) but it is still being enabled by something.
disabling Scroll_lock ends the behavior
(define-key global-map (kbd "<Scroll_Lock>") nil)
linking the key back to scroll-lock-mode will let this problem re-emerge.
I haven't really figured out what's behind such erroneous auto trigger of scroll-lock-mode though.

lsp-mode for go erring out with "no views in session"

What I'm trying to accomplish
I'd like to use go-mode/lsp-mode together. I struggle to get lsp-mode to even execute at first, finally got it to work by appending the paths :facepalm:.
The issue
The issue now is that when lsp-mode starts up for the working golang directory, I receive this error:
LSP :: Error from the Language Server: no views in the session (Unknown error) [3 times]
I've searched around the internet for answers, but have yet to find anything that is relevant to my problem. I'm reaching out to the community for some guidance.
go-mode.el
(defun custom-go-mode ()
(display-line-numbers-mode 1))
(use-package go-mode
:defer t
:ensure t
:mode ("\\.go\\'" . go-mode)
:init
(setq compile-command "echo Building... && go build -v && echo Testing... && go test -v && echo Linter... && golint")
(setq compilation-read-command nil)
(add-hook 'go-mode-hook 'custom-go-mode)
:bind (("M-," . compile)
("M-." . godef-jump)))
(setq compilation-window-height 14)
(defun my-compilation-hook ()
(when (not (get-buffer-window "*compilation*"))
(save-selected-window
(save-excursion
(let* ((w (split-window-vertically))
(h (window-height w)))
(select-window w)
(switch-to-buffer "*compilation*")
(shrink-window (- h compilation-window-height)))))))
(add-hook 'compilation-mode-hook 'my-compilation-hook)
(global-set-key (kbd "C-c C-c") 'comment-or-uncomment-region)
(setq compilation-scroll-output t)
lsp-mode.el
(setq exec-path (append exec-path '("/Users/seanh/.nvm/versions/node/v12.13.0/bin/npm")))
(setq exec-path (append exec-path '("/Users/seanh/.nvm/versions/node/v12.13.0/bin/")))
(setq exec-path (append exec-path '("/usr/local/bin")))
(setq exec-path (append exec-path '("/Users/seanh/go/bin")))
(setq exec-path (append exec-path '("/Users/seanh/go/bin/gopls")))
(setq exec-path (append exec-path '("/usr/local/go/bin/go")))
(use-package lsp-mode
:ensure t
:commands (lsp lsp-deferred)
:hook (go-mode . lsp-deferred))
;;Set up before-save hooks to format buffer and add/delete imports.
;;Make sure you don't have other gofmt/goimports hooks enabled.
(defun lsp-go-install-save-hooks ()
(add-hook 'before-save-hook #'lsp-format-buffer t t)
(add-hook 'before-save-hook #'lsp-organize-imports t t))
(add-hook 'go-mode-hook #'lsp-go-install-save-hooks)
;;Optional - provides fancier overlays.
(use-package lsp-ui
:ensure t
:commands lsp-ui-mode
:init
)
;;Company mode is a standard completion package that works well with lsp-mode.
;;company-lsp integrates company mode completion with lsp-mode.
;;completion-at-point also works out of the box but doesn't support snippets.
(use-package company
:ensure t
:config
(setq company-idle-delay 0)
(setq company-minimum-prefix-length 1))
(use-package company-lsp
:ensure t
:commands company-lsp)
;;Optional - provides snippet support.
(use-package yasnippet
:ensure t
:commands yas-minor-mode
:hook (go-mode . yas-minor-mode))
;;lsp-ui-doc-enable is false because I don't like the popover that shows up on the right
;;I'll change it if I want it back
(setq lsp-ui-doc-enable nil
lsp-ui-peek-enable t
lsp-ui-sideline-enable t
lsp-ui-imenu-enable t
lsp-ui-flycheck-enable t)
I've had this happen when the go environment is not correct.
As a diagnostic try commenting out the go* exec-paths from your lsp-mode.el and set appropriate environment variables (GOROOT, GOPATH and GO111MODULE if you're using it), including adding $GOROOT/bin and $GOPATH/bin to PATH.
Also if still not working verify within emacs those environment variables are set correctly.
For linux of course you can export all that and run emacs from the terminal to test.

ELISP: How to trap display-completion-list to a variable

I have a list of targets and I want to write a function where you can choose the
current target. My code looks like that below.
The problem is that when I do "M-x my-test", current_target is set as nil and the chosen
address is printed on the current buffer.
How do I trap the buffer output to current_target? Or my whole approach is wrong?
Please advice? Which doc to read?
Thanx
-Siddhartha
(defvar target-list '( ("10.25.110.113" " -> target-1")
("10.25.110.114" " -> target-2")) "List of Target boxes")
(defvar current-target "0.0.0.0" "Current target")
(defun my-test ()
(interactive)
(with-output-to-temp-buffer "*Target List*"
(princ "\nPlease click on IP address to choose the target\n\n")
(setq current-target (display-completion-list target-list))))
Not sure exactly what behavior you want. But if you just want to let a user choose one of your strings, then try using completing-read:
(defun my-test ()
(interactive)
(setq current-target (completing-read "Target: " target-list nil t)))
Or if you want to return the associated target then look up the string chosen in your alist:
(defun my-test ()
(interactive)
(let (target)
(setq current-target (completing-read "Target: " target-list nil t)
target (cdr (assoc current-target target-list)))
(message "Target: %s" target)))
You get the idea.
;; The code for the question after the reply from Drew is as follows
;; The idea is to present to the user names to choose from.
;; Thanx Drew for "giving the idea"
(defvar target-assoc-list '( ("Fire" . "10.25.110.113") ("Earth" . "10.25.110.114")
("Water" . "10.25.110.115") ("Air" . "10.25.110.116"))
"The assoc list of (name . ip-addr) so that user chooses by name
and current-target is assigned the ip address")
(defvar current-target "0.0.0.0")
(defun my-select-target ()
(interactive)
(let (name)
(setq name (completing-read "Enter Target (TAB for list): "
target-assoc-list nil t)
current-target (cdr (assoc name target-assoc-list)))
(message "Chosen current-target IP address: %s name: %s" current-target name)))

How do I fix a "Wrong Type Argument: listp," error when trying to access remote file using tramp?

I am unable to access remote files in my usual way:
C-x C-f
[server]:[path][file]
and am thrown this error:
Wrong Type Argument: listp, [[server]:[path][file]
I'm not even sure how to debug this further.
any help is appreciated.
edit:
output when trying to debug:
Debugger entered: nil
(progn (debug) (ido-mode t) (progn (ad-add-advice (quote completing-read) (quote (foo nil
t (advice lambda nil (if (boundp ...) ad-do-it (setq ad-return-value ...))))) (quote
around) (quote nil)) (ad-activate (quote completing-read) nil) (quote completing-read)) (define-key global-map [(meta 120)] (function (lambda nil (interactive) (call-interactively
(intern (ido-completing-read "M-x " (all-completions "" obarray ...))))))))
(if (fboundp (quote ido-mode)) (progn (debug) (ido-mode t) (progn (ad-add-advice (quote
completing-read) (quote (foo nil t (advice lambda nil (if ... ad-do-it ...)))) (quote
around) (quote nil)) (ad-activate (quote completing-read) nil) (quote completing-read))
(define-key global-map [(meta 120)] (function (lambda nil (interactive) (call-
interactively (intern (ido-completing-read "M-x " ...))))))))
eval-buffer() ; Reading at buffer position 16103
call-interactively(eval-buffer)
(lambda nil (interactive) (call-interactively (intern (ido-completing-read "M-x " (all-
completions "" obarray (quote commandp))))))()
call-interactively((lambda nil (interactive) (call-interactively (intern (ido-completing-
read "M-x " (all-completions "" obarray (quote commandp)))))) nil nil)
recursive-edit()
debug(debug)
implement-debug-on-entry()
* ido-find-file()
call-interactively(ido-find-file nil nil)
And this from my init.el:
(require 'ido)
(if (fboundp 'ido-mode)
(progn
(debug)
(ido-mode t)
(defadvice completing-read
(around foo activate)
(if (boundp 'ido-cur-list)
ad-do-it
(setq ad-return-value
(ido-completing-read
prompt
(all-completions "" collection predicate)
nil require-match initial-input hist def))))
(define-key global-map [(meta ?x)]
(lambda ()
(interactive)
(call-interactively
(intern
(ido-completing-read "M-x " (all-completions "" obarray 'commandp))))))))
Check what command C-x C-f is bound to (use C-h k). Is it the standard binding find-file? (It doesn't sound like it.)
If not, check its interactive spec. The command is expecting to receive a list as argument, and it is instead receiving (what looks like) a string.
This is the interactive spec of find-file:
(interactive
(find-file-read-args "Find file: " (confirm-nonexistent-file-or-buffer)))
If the interactive spec of your C-x C-f command, like this one, has a non-string as its argument, then you can either M-x debug-on-entry THE-FUNCTION, where THE-FUNCTION is the function called for the argument (find-file-read-args, in the case of find-file), or wrap that argument so that the debugger is invoked:
(progn (debug) (WHATEVER-WAS-THERE-BEFORE))
Either way, the debugger will open for the interactive part of reading the file name, and you can walk through the debugger to see what goes wrong.
But probably you can figure out the problem just by inspecting the code -- the interactive spec. The argument to your command (whatever it is) is expected to be a list, but it is a string.
I would start by seeing what happens with a local file name. Do you get an error for that too?
Another thing I notice is that the error reports an extra [, in front of what you say you typed as input. That should provide a clue too. What you think it is reading is not what it has read.

Open a file at line with "filename:line" syntax

Very often, compilations errors are displayed with the file:line syntax.
It would be nice to copy-paste this directly to open the file at the right line.
Emacs already has some mode to handle this in buffers (compile-mode, iirc), but I would like to have this available from the shell command line, since I use the standard shell most of the time outside of emacs.
Any idea how to tweak emacs to learn file:line syntax to open file at line line? (obviously, if file:line really exists on disk, it should be opened preferably)
You can do this using emacsclient. e.g. to open FILE at line 4, column 3 in a new frame:
emacsclient +4:3 FILE
Leave off the :3 to simply open the file at line 4.
I have the following in my .emacs, but I haven't found it as useful as I thought it would be.
;; Open files and goto lines like we see from g++ etc. i.e. file:line#
;; (to-do "make `find-file-line-number' work for emacsclient as well")
;; (to-do "make `find-file-line-number' check if the file exists")
(defadvice find-file (around find-file-line-number
(filename &optional wildcards)
activate)
"Turn files like file.cpp:14 into file.cpp and going to the 14-th line."
(save-match-data
(let* ((matched (string-match "^\\(.*\\):\\([0-9]+\\):?$" filename))
(line-number (and matched
(match-string 2 filename)
(string-to-number (match-string 2 filename))))
(filename (if matched (match-string 1 filename) filename)))
ad-do-it
(when line-number
;; goto-line is for interactive use
(goto-char (point-min))
(forward-line (1- line-number))))))
I suggest to add following code in your emacs config:
(defadvice server-visit-files (before parse-numbers-in-lines (files proc &optional nowait) activate)
"looks for filenames like file:line or file:line:position and reparses name in such manner that position in file"
(ad-set-arg 0
(mapcar (lambda (fn)
(let ((name (car fn)))
(if (string-match "^\\(.*?\\):\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?$" name)
(cons
(match-string 1 name)
(cons (string-to-number (match-string 2 name))
(string-to-number (or (match-string 3 name) "")))
)
fn))) files))
)
by now you can open file with a line number right from command line like this:
emacsclient filename:linenumber:position
P.S. I hope i'm not too late with my answer.
And here is my go at it. Calls the original find-file-at-point
(defun find-file-at-point-with-line()
"if file has an attached line num goto that line, ie boom.rb:12"
(interactive)
(setq line-num 0)
(save-excursion
(search-forward-regexp "[^ ]:" (point-max) t)
(if (looking-at "[0-9]+")
(setq line-num (string-to-number (buffer-substring (match-beginning 0) (match-end 0))))))
(find-file-at-point)
(if (not (equal line-num 0))
(goto-line line-num)))
You can use a bash script:
#! /bin/bash
file=$(awk '{sub(/:[0-9]*$/,"")}1' <<< "$1")
line=$(awk '{sub(/^.*:/,"")}1' <<< "$1")
emacs --no-splash "+$line" "$file" &
If you call this script for openline and you get an error message, e.g.
Error: file.cpp:1046
you can do
openline file.cpp:1046
to open the file.cpp in Emacs at line 1046..
Another version of Ivan Andrus' nice find-file advice which does both line + optional column number, as you see in node and coffeescript errors:
;; Open files and go places like we see from error messages, i e: path:line:col
;; (to-do "make `find-file-line-number' work for emacsclient as well")
;; (to-do "make `find-file-line-number' check if the file exists")
(defadvice find-file (around find-file-line-number
(path &optional wildcards)
activate)
"Turn files like file.js:14:10 into file.js and going to line 14, col 10."
(save-match-data
(let* ((match (string-match "^\\(.*?\\):\\([0-9]+\\):?\\([0-9]*\\)$" path))
(line-no (and match
(match-string 2 path)
(string-to-number (match-string 2 path))))
(col-no (and match
(match-string 3 path)
(string-to-number (match-string 3 path))))
(path (if match (match-string 1 path) path)))
ad-do-it
(when line-no
;; goto-line is for interactive use
(goto-char (point-min))
(forward-line (1- line-no))
(when (> col-no 0)
(forward-char (1- col-no)))))))
Emacs 25 does not use defadvice anymore. Refs.
So here is the version updated to the new syntax:
(defun find-file--line-number (orig-fun filename &optional wildcards)
"Turn files like file.cpp:14 into file.cpp and going to the 14-th line."
(save-match-data
(let* ((matched (string-match "^\\(.*\\):\\([0-9]+\\):?$" filename))
(line-number (and matched
(match-string 2 filename)
(string-to-number (match-string 2 filename))))
(filename (if matched (match-string 1 filename) filename)))
(apply orig-fun (list filename wildcards))
(when line-number
;; goto-line is for interactive use
(goto-char (point-min))
(forward-line (1- line-number))))))
(advice-add 'find-file :around #'find-file--line-number)
This works if you call open file from inside emacs (C-x C-f), but not works anymore from command line, it seems that emacs 25 is not using find-file when you call it from command line and I don't know how to debug this kind of thing.
You talk about pasting to open a file (I assume you mean at a find file prompt inside of emacs) and also doing something from the command line. If you want to copy & paste then you need to do something like what Ivan showed with the defadvice. If you want something from the command line you can do the following. I've adapted this from something I did a year ago with an emacs:// URI handler (for use from within Firefox):
Put this in your .emacs file:
(defun emacs-uri-handler (uri)
"Handles emacs URIs in the form: emacs:///path/to/file/LINENUM"
(save-match-data
(if (string-match "emacs://\\(.*\\)/\\([0-9]+\\)$" uri)
(let ((filename (match-string 1 uri))
(linenum (match-string 2 uri)))
(while (string-match "\\(%20\\)" filename)
(setq filename (replace-match " " nil t filename 1)))
(with-current-buffer (find-file filename)
(goto-line (string-to-number linenum))))
(beep)
(message "Unable to parse the URI <%s>" uri))))
and then create a shell script in your path (I called mine 'emacsat'):
#!/bin/bash
emacsclient --no-wait -e "(emacs-uri-handler \"emacs://$1/${2:-1}\")"
A DOS batch script would look similar, but I don't know how to do default values (though I'm pretty sure you can do it).
See How to configure firefox to run emacsclientw on certain links? for further instructions if you want to integrate with Firefox, too.
To return42's code, added column number support, and cleaned up case where column number is present, and line number is sought.
;; find file at point, jump to line no.
;; ====================================
(require 'ffap)
(defun find-file-at-point-with-line (&optional filename)
"Opens file at point and moves point to line specified next to file name."
(interactive)
(let* ((filename (or filename (if current-prefix-arg (ffap-prompter) (ffap-guesser))))
(line-number
(and (or (looking-at ".* line \\(\[0-9\]+\\)")
(looking-at "[^:]*:\\(\[0-9\]+\\)"))
(string-to-number (match-string-no-properties 1))))
(column-number
(or
(and (looking-at "[^:]*:\[0-9\]+:\\(\[0-9\]+\\)")
(string-to-number (match-string-no-properties 1)))
(let 'column-number 0))))
(message "%s --> %s:%s" filename line-number column-number)
(cond ((ffap-url-p filename)
(let (current-prefix-arg)
(funcall ffap-url-fetcher filename)))
((and line-number
(file-exists-p filename))
(progn (find-file-other-window filename)
;; goto-line is for interactive use
(goto-char (point-min))
(forward-line (1- line-number))
(forward-char column-number)))
((and ffap-pass-wildcards-to-dired
ffap-dired-wildcards
(string-match ffap-dired-wildcards filename))
(funcall ffap-directory-finder filename))
((and ffap-dired-wildcards
(string-match ffap-dired-wildcards filename)
find-file-wildcards
;; Check if it's find-file that supports wildcards arg
(memq ffap-file-finder '(find-file find-alternate-file)))
(funcall ffap-file-finder (expand-file-name filename) t))
((or (not ffap-newfile-prompt)
(file-exists-p filename)
(y-or-n-p "File does not exist, create buffer? "))
(funcall ffap-file-finder
;; expand-file-name fixes "~/~/.emacs" bug sent by CHUCKR.
(expand-file-name filename)))
;; User does not want to find a non-existent file:
((signal 'file-error (list "Opening file buffer"
"no such file or directory"
filename))))))
I made a little rewrite of the find-file-at-point function.
If there is a line number match, the file will be opened within an other window and the courser will be placed in this line. If no line number match, do what ffap normally does ...
;; find file at point, jump to line no.
;; ====================================
(require 'ffap)
(defun find-file-at-point-with-line (&optional filename)
"Opens file at point and moves point to line specified next to file name."
(interactive)
(let* ((filename (or filename (ffap-prompter)))
(line-number
(and (or (looking-at ".* line \\(\[0-9\]+\\)")
(looking-at ".*:\\(\[0-9\]+\\):"))
(string-to-number (match-string-no-properties 1)))))
(message "%s --> %s" filename line-number)
(cond ((ffap-url-p filename)
(let (current-prefix-arg)
(funcall ffap-url-fetcher filename)))
((and line-number
(file-exists-p filename))
(progn (find-file-other-window filename)
(goto-line line-number)))
((and ffap-pass-wildcards-to-dired
ffap-dired-wildcards
(string-match ffap-dired-wildcards filename))
(funcall ffap-directory-finder filename))
((and ffap-dired-wildcards
(string-match ffap-dired-wildcards filename)
find-file-wildcards
;; Check if it's find-file that supports wildcards arg
(memq ffap-file-finder '(find-file find-alternate-file)))
(funcall ffap-file-finder (expand-file-name filename) t))
((or (not ffap-newfile-prompt)
(file-exists-p filename)
(y-or-n-p "File does not exist, create buffer? "))
(funcall ffap-file-finder
;; expand-file-name fixes "~/~/.emacs" bug sent by CHUCKR.
(expand-file-name filename)))
;; User does not want to find a non-existent file:
((signal 'file-error (list "Opening file buffer"
"no such file or directory"
filename))))))
If you have an old version of the ffap (2008) you should update your emacs or apply
an other small patch ...
--- Emacs/lisp/ffap.el
+++ Emacs/lisp/ffap.el
## -1170,7 +1170,7 ## which may actually result in an url rather than a filename."
;; remote, you probably already have a connection.
((and (not abs) (ffap-file-exists-string name)))
;; Try stripping off line numbers; good for compilation/grep output.
- ((and (not abs) (string-match ":[0-9]" name)
+ ((and (string-match ":[0-9]" name)
(ffap-file-exists-string (substring name 0 (match-beginning 0)))))
;; Try stripping off prominent (non-root - #) shell prompts
Here is a zsh function that works if you put it into your .zshrc file.
Since I'm running my code in zsh usually, and this is where i see the errors. Kudos to #sanityinc for the emacs part. Just thought this should be on google.
emn () {
blah=$1
filen=(${(s.:.)blah})
/Applications/Emacs.app/Contents/MacOS/Emacs +$filen[2] $filen[1] &
}
Use like emn /long/stack/error.rb:123
I've modified ivan-andrus defadvice so it works with emacsclient:
(defadvice find-file-noselect (around find-file-noselect-at-line
(filename &optional nowarn rawfile wildcards)
activate)
"Turn files like file.cpp:14 into file.cpp and going to the 14-th line."
(save-match-data
(let* ((matched (string-match "^\\(.*\\):\\([0-9]+\\):?$" filename))
(line-number (and matched
(match-string 2 filename)
(string-to-number (match-string 2 filename))))
(filename (if matched (match-string 1 filename) filename))
(buffer-name ad-do-it))
(when line-number
(with-current-buffer buffer-name
(goto-char (point-min))
(forward-line (1- line-number)))))))
I use this a lot, I made it with command-line-1:
;;;; Open files and goto lines like we see from g++ etc. i.e. file:line#
(defun command-line-1--line-number (orig-fun args)
(setq new-args ())
(dolist (f args)
(setq new-args
(append new-args
(if (string-match "^\\(.*\\):\\([0-9]+\\):?$" f)
(list (concat "+" (match-string 2 f))
(match-string 1 f))
(list f)))))
(apply orig-fun (list new-args)))
(advice-add 'command-line-1 :around #'command-line-1--line-number)

Resources