Getting the output of a process - elisp

I am trying to create a function which takes as input a path and returns the output of the ls terminal command as a string. I'm using a process and sentinel since I'll eventually want to create other functions which will take some time to execute, and I want them to run asynchronously.
(defun ls-to-string (path)
(let (ls-proc
ls-output)
(progn (setq ls-proc (start-process "" "ls-buffer" "ls" path))
(set-process-sentinel ls-proc (lambda (p e)
(if (string= e "finished\n")
(progn (set-buffer "ls-buffer")
(setq ls-output (buffer-string))
(kill-buffer "ls-buffer")
(message ls-output))))) <---- (1)
ls-output))) <---- (2)
(ls-to-string "/home")
I have (temporarily) added (message ls-output) just to show that ls-output does contain the string (1). However the return value is nil (2).

Related

Emacs: Symbol's value as variable is void: Removes (init.el)

Sorry if this is a silly question; I am a complete novice when it comes to emacs.
Recently, I began to do research on how to set up emacs and stumbled upon a great video series by Mike Zamansky. However, whilst following this video (creating an org init file), all of the packages I installed onto my emacsclient proceeded to not work. During initialization, there was an error - namely, "Symbol's value as variable is void: Removes." I copied his tutorial verbatim and I don't see any potential syntactical errors - perhaps I overlooked some errors. However, I've been searching throughout the internet, but could not find any answers to this problem.
Here is the contents of the init.el file:
(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives
'("melpa" . "http://melpa.org/packages/"))
(package-initialize)
;; Bootstrap 'use-package'
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(org-babel-load-file (expand-file-name "~/.emacs.d/myinit.org"))
Here is the error:
Warning (initialization): An error occurred while loading
‘/Users/Kyojin/.emacs.d/init.el’:
Symbol's value as variable is void: Removes
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the ‘--debug-init’ option to view a complete error backtrace.
Debugger Output (--debug-init):
Debugger entered--Lisp error: (void-variable Removes)
eval-buffer(#<buffer *load*-527594> nil
"/Users/Kyojin/.emacs.d/myinit.el" nil t) ; Reading at buffer position
8
load-with-code-conversion("/Users/Kyojin/.emacs.d/myinit.el"
"/Users/Kyojin/.emacs.d/myinit.el" nil nil)
load("/Users/Kyojin/.emacs.d/myinit.el" nil nil t)
load-file("/Users/Kyojin/.emacs.d/myinit.el")
(progn (load-file exported-file) "Loaded")
(if compile (progn (byte-compile-file exported-file (quote load))
"Compiled and loaded") (progn (load-file exported-file) "Loaded"))
(message "%s %s" (if compile (progn (byte-compile-file exported-file
(quote load)) "Compiled and loaded") (progn (load-file exported-file)
"Loaded")) exported-file)
(let* ((age (function (lambda (file) (float-time (time-subtract
(current-time) (nth 5 ...)))))) (base-name (file-name-sans-extension
file)) (exported-file (concat base-name ".el"))) (if (and (file-exists-
p exported-file) (> (funcall age file) (funcall age exported-file)))
nil (setq exported-file (car (last (org-babel-tangle-file file
exported-file "emacs-lisp"))))) (message "%s %s" (if compile (progn
(byte-compile-file exported-file (quote load)) "Compiled and loaded")
(progn (load-file exported-file) "Loaded")) exported-file))
org-babel-load-file("/Users/Kyojin/.emacs.d/myinit.org")
eval-buffer(#<buffer *load*> nil "/Users/Kyojin/.emacs.d/init.el"
nil t) ; Reading at buffer position 358
load-with-code-conversion("/Users/Kyojin/.emacs.d/init.el"
"/Users/Kyojin/.emacs.d/init.el" t t)
load("/Users/Kyojin/.emacs.d/init" t t)
#[0"\205\266\306=\203\307\310Q\202?\311=\204\307\312Q\202?\313\307
\314\315#\203* \316\202?\313\307\314\317#\203>\320\321\322!D\nB\323
\202?\316\324\325\324\211#\210\324=\203e\326\327\330\307\331Q!\"\325
\324\211#\210\324=\203d\210\203\247\332!\333\232\203\247\334!
\211\335P\336!\203\201\211\202\214\336!\203\213\202\214\314\262\
\203\245\337\"\203\243\340\341#\210\342\343!\210\266\f\205\264\314\325
\344\324\211#)\262\207"[init-file-user system-type
delayed-warnings-list user-init-file inhibit-default-init inhibit-
startup-screen ms-dos "~" "/_emacs" windows-nt "/.emacs" directory-
files nil "^\\.emacs\\(\\.elc?\\)?$" "~/.emacs" "^_emacs\\(\\.elc?\\)?
$" initialization format-message "`_emacs' init file is deprecated,
please use `.emacs'" "~/_emacs" t load expand-file-name "init" file-
name-as-directory "/.emacs.d" file-name-extension "elc" file-name-sans-
extension ".el" file-exists-p file-newer-than-file-p message "Warning:
%s is newer than %s" sit-for 1 "default"] 7]()
command-line()
normal-top-level()
Search for the word Removes in your init file, "/Users/Kyojin/.emacs.d/myinit.el" (or possibly in some file that it loads).
If you don't find it immediately then recursively bisect your init file to find the code that is problematic. You can do that by commenting out first 1/2, then 3/4, then 7/8,... of the file until you locate the problem. You can comment a block of text/code by selecting it and then using M-x comment-region. You can uncomment a selection the same way, but with C-u first: C-u M-x comment-region.

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.

How can I map an unknown list of args to start-process in elisp?

I'm finally trying to learn elisp but haven't wrapped my head around how to map an unknown list of arguments to variables dynamically.
Here's a working function that passes up to three arguments to start-process. But I would like to pass an infinite number of args to the function.
(defun create-drush-buffer (command &rest a)
(if (locate-dominating-file default-directory "includes/bootstrap.inc")
(progn
(setq opt1 (car a))
(setq opt2 (cadr a))
(setq opt3 (caddr a))
(setq allopt (concat opt1 " " opt2 " " opt3))
(setq b-name (concat "*drush " command " " allopt "*"))
(if (buffer-live-p b-name)
(switch-to-buffer b-name)
(setq d-buffer (get-buffer-create b-name))
(with-current-buffer d-buffer
(goto-char (point-min))
(view-mode 1)
(hl-line-mode 1)
(if opt3
(start-process "drush" (current-buffer) drupal-drush-program
command
opt1
opt2
opt3)
(if opt2
(start-process "drush" (current-buffer)
drupal-drush-program
command
opt1
opt2)
(if opt1
(start-process "drush" (current-buffer)
drupal-drush-program
command
opt1)
(start-process "drush" (current-buffer)
drupal-drush-program
command))))
(shrink-window-if-larger-than-buffer))
(switch-to-buffer d-buffer)))
(message (concat default-directory " is not a drupal project"))))
Here's an example of a calling function I would like to work with create-drush-buffer.
(defun drush-sql-sync ()
(interactive)
(create-drush-buffer
"sql-sync"
"-y"
"-d"
"-v"
"#cu.wstage1-education"
"#cu.local-education"))
How can I achieve this and make my code less redundant? Any help with code or even steering me to relevant documentation appreciated.
You can use apply:
(apply FUNCTION &rest ARGUMENTS)
Call FUNCTION with our remaining args, using our last arg as list of
args. Then return the value FUNCTION returns. Thus, (apply '+ 1 2 '(3
4)) returns 10.
This way, you could call start-process using something like:
(apply 'start-process "drush" (current-buffer)
drupal-drush-program
command
a)
As a side note, you should not create temporary variables using setq, as this creates or modified global variables (if no local ones with the name exists). Instead, use let.
Good luck with your elisp projects!

How to wait for / capture aysnchronous shell command output in emacs lisp?

If I execute a shell command asynchronously in emacs lisp like so:
(shell-command "mycommand &")
Is there a way to wait for the command to generate output before continuing? For my current application, it is probably sufficient to wait until the command generates any output at all, but ideally I'd like to capture the output for additional processing. Is this possible?
You should use comint-output-filter-functions variable that contains function to call after output is inserted into the buffer.
For example, you can do :
(add-hook 'comint-output-filter-functions '(lambda (txt) (message "hello")))
N.B. : From Emacs 23.2, you have the new command async-shell-command, bound globally to M-&.
This executes your command asynchronously without requiring an ampersand. The output of your command is sent to the buffer
*Async Shell Command*.
Perhaps you need to register a Process Filter to give you the callback timing you need? See 37.9 Receiving Output from Processes in the Elisp manual (I see this in my copy for Emacs 22.3).
Here is an example of running a callback when you get the first process output and also storing it into an "associated buffer". Copy it to your *scratch* buffer and eval-region it, but make sure to split-window and show the *Messages* buffer visible so that you can see what's going on.
;; this is emacs lisp (and a comment line)
(defvar my-callback-got-some-already nil)
(defun my-callback ()
(message "callback ran at %s" (current-time-string)))
(defun my-filter-waits-for-first-time-input (proc string)
(unless my-callback-got-some-already
(setq my-callback-got-some-already t)
;; do your one-time thing
(my-callback))
;; insert into the associated buffer as if no process filter was
;; registered
(with-current-buffer (process-buffer proc)
(let ((moving (= (point) (process-mark proc))))
(save-excursion
;; Insert the text, advancing the process marker.
(goto-char (process-mark proc))
(insert string)
(set-marker (process-mark proc) (point)))
(if moving (goto-char (process-mark proc))))))
(defun async-callback-test-harness ()
(interactive)
(let ((process-handle "async-callback-test")
(associated-process-buffer "*async-callback-test*")
(command "ls")
(busy-loop-var ""))
(setq my-callback-got-some-already nil)
(message "start test %s" (current-time-string))
(start-process process-handle associated-process-buffer command)
;; Supposedly async but Emacs doesn't get the input until
;; "Emacs is waiting" so the following set-process-filter
;; can be registered in time.
;; To prove the point, make emacs busy loop to show that the
;; emacs doesn't drop its input and
;; the callback will get the unskipped input.
(switch-to-buffer associated-process-buffer)
(dotimes (k 2000) ; about two seconds on my machine
(setq busy-loop-var (concat busy-loop-var "busy looping...")))
(message "done busy waiting %s" (current-time-string))
(set-process-filter (get-process process-handle)
'my-filter-waits-for-first-time-input)
nil))
;; run it!
(async-callback-test-harness)

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