I'm having a hard time finding the documentation for this. How do I read/write text from the current buffer in my vim functions?
More concretely, if my buffer contains the words foo bar how would write a function to overwrite the word bar with cat so that in the end my buffer contains foo cat?
You can use the substitute ex command inside a function. For example
function! ReplaceBar()
:%s/bar/cat/g
endfunction
This defines a function. The % character means operate on the entire buffer. This searches for bar, replaces it with cat, and the g flag replaces every instance on a line, not just the first.
You can run this function by typing :call ReplaceBar() and hitting enter. Often it's convenient to define a function that does this kind of work, then define a command that calls it:
command! -nargs=0 Bar call ReplaceBar()
That command can be run by typing :Bar.
To access line(s), you can use the getline() function. setline() updates those lines in the buffer. Likewise, new lines are inserted via append().
The latter can also be done with :put ={variable or expression}, and replacements with :substitute. What is better depends on the particular use case. The benefit of the former, lower-level functions is that they don't clobber stuff like the expression register, last used search pattern, search history, etc.
In neovim, they have nvim_put. A few examples:
:call nvim_put(['cat'], 'c', v:false, v:true) ; insert 'cat' right where the cursor is, as if you typed `:normal! icat`
:call nvim_put(['cat'], 'l', v:true, v:true) ; insert 'cat' on the next line
Others have covered the use of :put pretty well, so I won't cover it myself.
Related
Acordding to the documentation of maxima, with_stdout is a function that evaluates some expressions and writes the output according to this expressions to a file f. I tried to use this function with a simple example:
with_stdout ("data.txt", for x:0 thru 10 do print (x, x^2, x^3))$
But the output look like this:
<mth><n>0</n><st> </st><n>0</n><st> </st><n>0</n><st> </st></mth><mth><n>1</n><st> </st><n>1</n><st> </st><n>1</n><st> </st></mth><mth><n>2</n><st> </st><n>4</n><st> </st><n>8</n><st> </st></mth><mth><n>3</n><st> </st><n>9</n><st> </st><n>27</n><st> </st></mth><mth><n>4</n><st> </st><n>16</n><st> </st><n>64</n><st> </st></mth><mth><n>5</n><st> </st><n>25</n><st> </st><n>125</n><st> </st></mth><mth><n>6</n><st> </st><n>36</n><st> </st><n>216</n><st> </st></mth><mth><n>7</n><st> </st><n>49</n><st> </st><n>343</n><st> </st></mth><mth><n>8</n><st> </st><n>64</n><st> </st><n>512</n><st> </st></mth><mth><n>9</n><st> </st><n>81</n><st> </st><n>729</n><st> </st></mth><mth><n>10</n><st> </st><n>100</n><st> </st><n>1000</n><st> </st></mth>
instead of writting a table with three columns as it is supposed to do.
I don't even understand the first output. What I am missunderstanding or missing here?
--
It seems there is a bug triggered by Wxmaxima, I don't know if it is on maxima aswell.
Regards.
Apparently wxMaxima overrides the default print function to generate XML tags (stuff like <foo> ... </foo>) which wxMaxima uses to indicate how stuff is displayed. I don't know if it's possible to directly call the default print function in wxMaxima; maybe, maybe not.
I can see a few options. (1) Call grind instead, which outputs the so-called 1-dimensional output. That's probably more suitable for file output anyway.
(2) Call printf, e.g. printf(true, "~a, ~a, ~a~%", x, x^2, x^3). printf recognizes many output options, as described by ? printf. It's possible printf calls are also intercepted by wxMaxima, I haven't tried it.
(3) Use the plain text, console Maxima interface, then print is sure to be the default.
I've got a file with syntactically correct Lua 5.1 source code.
I've got a position (line and character offset) inside that file.
I need to get an offset in bytes to the closing parenthesis of the innermost function() body that contains that position (or figure out that the position belongs to the main chunk of the file).
I.e.:
local function foo()
^ result
print("bar")
^ input
end
local foo = function()
^ result
print("bar")
^ input
end
local foo = function()
return function()
^ result
print("bar")
^ input
end
end
...And so on.
How do I do that robustly?
EDIT: My original answer did not take into account the "innermost" requirement. I've since taken that into account
To make things "robust," there are a few considerations.
First of all, it's important that you skip over string and comment contents, to avoid incorrect output in situations like:
foo = function()
print(" function() ")
-- function()
print("bar")
^ input
end
This can be somewhat difficult, considering Lua's nested string and comment syntax. Consider, for example, a situation where the input begins in a nested string or comment:
foo = function()
print([[
bar = function()
print("baz")
^ input
end
]])
end
Consequently, if you want a completely robust system, it is not acceptable to only parse backwards until you hit the end of a function parameter list, because you may not have parsed backwards far enough to reach a [[ which would invalidate your match. It is therefore necessary to parse the entire file up to your position (unless you're okay with incorrect matches in these weird situations. If this is an editor plugin, these "incorrect" results may actually be desirable, because they would allow you to edit lua code which is stored in string literal form inside other lua code using the same plugin).
Because the particular syntax that you're trying to match doesn't have any kind of "nesting", a full-blown parser isn't needed. You will need to maintain a stack, however, to keep track of scope. With that in mind, all you need to do is step through the source file character-by-character from the beginning, applying the following logic:
Every time a " or ' is encountered, ignore the characters up to the closing " or '. Be careful to handle escapes like \" and \\
Every time a -- is encountered, ignore the characters up to the closing newline for the comment. Be careful to only do this if the comment is not a multiline comment.
Every time a multiline string opening symbol is encountered (such as [[, [=[, etc), or a multiline comment symbol is encountered (such as --[[ or --[=[, etc) ignore the characters up until the closing square brackets with the proper number of matching equals signs between them.
When a word boundary is encountered check to see if the characters after it could begin a block which ends with an end (for example, if, while, for, function, etc. DO NOT include repeat). If so, push the position on the scope stack. A "word boundary" in this case is any character which could not be used a lua identifier (this is to prevent matches in cases like abcfunction()). The beginning of the file is also considered a word boundary.
If a word boundary is encountered and it is followed by end, pop the top element of the stack. If the stack has no elements, complain about a syntax error.
When you finally step forward and reach your "input" position, pop elements from the stack until you find a function scope. Step forward from that position to the next ), ignoring )'s in comments (which could theoretically be found in an argument list if it spans multiple lines or contains inline --[[ ]] comments). That position is your result.
This should handle every case, including situations where the function syntactic sugar is used, like
function foo()
print("bar")
end
which you did not include in your example but which I imagine you still want to match.
I am relatively new to maxima. I want to know how to write an array into a text file using maxima.
I know it's late in the game for the original post, but I'll leave this here in case someone finds it in a search.
Let A be a Lisp array, Maxima array, matrix, list, or nested list. Then:
write_data (A, "some_file.data");
Let S be an ouput stream (created by openw or opena). Then:
write_data (A, S);
Entering ?? numericalio at the input prompt, or ?? write_ or ?? read_, will show some info about this function and related ones.
I've never used maxima (or even heard of it), but a little Google searching out of curiousity turned up this: http://arachnoid.com/maxima/files_functions.html
From what I can gather, you should be able to do something like this:
stringout("my_new_file.txt",values);
It says the second parameter to the stringout function can be one or more of these:
input: all user entries since the beginning of the session.
values: all user variable and array assignments.
functions: all user-defined functions (including functions defined within any loaded packages).
all: all of the above. Such a list is normally useful only for editing and extraction of useful sections.
So by passing values it should save your array assignments to file.
A bit more necroposting, as google leads here, but I haven't found it useful enough. I've needed to export it as following:
-0.8000,-0.8000,-0.2422,-0.242
-0.7942,-0.7942,-0.2387,-0.239
-0.7776,-0.7776,-0.2285,-0.228
-0.7514,-0.7514,-0.2124,-0.212
-0.7168,-0.7168,-0.1912,-0.191
-0.6750,-0.6750,-0.1655,-0.166
-0.6272,-0.6272,-0.1362,-0.136
-0.5746,-0.5746,-0.1039,-0.104
So I've found how to do this with printf:
with_stdout(filename, for i:1 thru length(z_points) do
printf (true,"~,4f,~,4f,~,4f,~,3f~%",bot_points[i],bot_points[i],top_points[i],top_points[i]));
A bit cleaner variation on the #ProdoElmit's answer:
list : [1,2,3,4,5]$
with_stdout("file.txt", apply(print, list))$
/* 1 2 3 4 5 is then what appears in file.txt */
Here the trick with apply is needed as you probably don't want to have square brackets in your output, as is produced by print(list).
For a matrix to be printed out, I would have done the following:
m : matrix([1,2],[3,4])$
with_stdout("file.txt", for row in args(m) do apply(print, row))$
/* 1 2
3 4
is what you then have in file.txt */
Note that in my solution the values are separated with spaces and the format of your values is fixed to that provided by print. Another caveat is that there is a limit on the number of function parameters: for example, for me (GCL 2.6.12) my method does not work if length(list) > 64.
I usually use Geany or Hi-Tide under Debian (GNU/Linux) for firmware development, mainly C (but also reading old assembler). I document code using single-line comments, and it really annoys me when I retype something and have to manually re-break every following line to keep it in the 80-character margin.
Is there a text editor that can re-wrap consecutive single-line comments (and do this automatically while I type)? That is, given:
/// This is a really long line that should have been wrapped at "that" but was not.
/// This sentence is in the same
/// paragraph as the last.
...I want an editor that will re-wrap this to
/// This is a really long line that
/// should have been wrapped at "that"
/// but was not. This sentence is in
/// the same paragraph as the last.
...preferably doing this sensibly while I type.
I've tried:
Hi-Tide (based on Eclipse 3.3)
Geany
jEdit
UniversalIndentGUI + a bunch of prettifiers (I couldn't find any formatters that worked, and it's not a great workflow either)
GVim - next line begins //should have been... instead of /// should have been...
Update: just to elaborate on my accepted answer - I've gone with the snapshot emacs and an extra filladapt mode was also required
In Emacs, to start automatic wrapping, enter auto-fill-mode. To set the line width, run C-u ⟨columns⟩ C-x f.
Emacs, or really CC Mode, will anticipate your commenting structure, so that typing
/// This is a really long line that shoul will result in
/// This is a really long line that
/// shoul‸
And you can refill a paragraph at any time with M-q.
If you want to do refills automatically with each keypress, well there may well be some interal command or third-party library out there, but off-hand you can use this elisp code:
;;; Can't advise SELF-INSERT-COMMAND, so create a wrapper procedure.
(defun self-insert-refill (n)
(interactive "p")
(self-insert-command n))
;;; Advise SELF-INSERT-REFILL to execute FILL-PARAGRAPH after every
;;; keypress, but *only* if we're inside a comment
(defadvice self-insert-refill (after refill-paragraph)
(let ((face (or (get-char-property (point) 'read-face-name)
(get-char-property (point) 'face))) )
(if (and (eq face 'font-lock-comment-face)
(not (string= " " (this-command-keys)))) ; Spaces would get deleted on refill.
(fill-paragraph))))
(ad-activate 'self-insert-refill)
(add-hook 'c-mode-hook
;; Remap SELF-INSERT-COMMAND to be SELF-INSERT-REFILL.
(local-set-key [remap self-insert-command] 'self-insert-refill) ))
This is probably not very robust or in keeping with best-practice, and likely not wholly satisfactory, as it won't work for general editing, e.g. C-d and backspace, and it slows down the editor somewhat, but it's a start.
Vim most certainly can do this.
First, you need to tell Vim that "///" is a comment prefix (it isn't by default):
:set comments^=:///
If you want wrapping to occur as-you-type, set your preferred textwidth:
:set textwidth=80
To format existing paragraphs, use any variation of the gq command. For example, you could:
Select a paragraph visually and type gq, or
Type gqj to re-wrap from the current line to the end of the paragraph
I spent some time trying to write a 'helper' macro to test a parameter for a new value, else use the existing value -- default values exist for all parameter positions.
I wanted to be able to write:
\foo{left}{nil}{}{20pt}
so that the second parameter would used its current value but the third value would be the value empty string. I wanted to use the notation:
\edef\pA{\isnil{#1}{\pA){#1}} % one for each parameter
I defined \isnil like so:
\def\nil{nil}
\def\isnil#1#2#3{%
\edef\nilTest{#1}%
\ifx\nilTest\nil#2\else#3\fi
}
but when I tried to run it, TeX complained that \nilTest is an undefined control sequence.
That is true of course, but I want \pA to hold a value, not a recipe for a value, so it must be an \edef which means that all the macro test will be expanded but while will the \edef not protect the \nilTest -- is this a place to use \noexpand -- that did not seem to work for me.
EDIT: no digits in \cs names (yeah, I knew that.)
Why doesn't your solution work? \edef\pA{\isnil{#1}{\pA){#1}} expands \isnil and gets \edef\nilTest{.... Now \edef is not expandable and falls into a sequence of \pA as the first element. An attempt to expand the next macro \nilTest fails.
Use \setpar from the following code to change your parameter.
\def\nil{nil}
\def\setpar#1#2{%
\edef\nilTest{#2}%
\ifx\nilTest\nil\else\let#1\nilTest\fi}
\def\first{old first}
\def\second{old second}
\setpar \first{nil}
\setpar \second{new}
first = ``\first'', second = ``\second''
P.S. Do not use digits in your macro.